porsager / porsager/postgres

Cloudflare Next.js Database error

Open
#781 4 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

connection.ts

import postgres from 'postgres';

const pgSqlClient = postgres({
  database: 'anchor',
  host: process.env['PG_DB_HOST'],
  user: 'postgres',
  password: process.env['PG_DB_PASSWORD'],
  port: 5432,
  ssl: {
    rejectUnauthorized: false,
  },
});

export default pgSqlClient;

using in route.ts

export const runtime = 'edge';
import pgSqlClient from '@/utils/db';
import { NextResponse } from 'next/server';

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url);
  const user_id = searchParams.get('user_id');
  try {
    if (!user_id) {
      return NextResponse.json(
        {
          message: 'bad request',
          data: null,
        },
        { status: 400 }
      );
    }
    const dbRes =
      await pgSqlClient`SELECT * from public.user WHERE id=${+user_id}`;
    if (dbRes.count > 0) {
      return NextResponse.json(
        {
          message: 'success',
          data: dbRes,
        },
        { status: 200 }
      );
    } else {
      return NextResponse.json(
        {
          message: 'Not Found',
          data: null,
        },
        { status: 404 }
      );
    }
  } catch (error) {
    return NextResponse.json(
      {
        message: 'Internal Server Error',
        data: null,
        error: error,
      },
      { status: 500 }
    );
  }
}

Error

00:56:15.856	▲  ▲ Next.js 14.0.4
00:56:15.856	▲  Creating an optimized production build ...
00:56:27.263	▲  Failed to compile.
00:56:27.263	▲  cloudflare:sockets
00:56:27.263	▲  Module build failed: UnhandledSchemeError: Reading from "cloudflare:sockets" is not handled by plugins (Unhandled scheme).
00:56:27.263	▲  Webpack supports "data:" and "file:" URIs by default.
00:56:27.263	▲  You may need an additional plugin to handle "cloudflare:" URIs.
00:56:27.264	▲  at /opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:399825
00:56:27.264	▲  at Hook.eval [as callAsync] (eval at create (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:13:28867), <anonymous>:6:1)
00:56:27.264	▲  at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:13:26021)
00:56:27.264	▲  at Object.processResource (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:399750)
00:56:27.264	▲  at processResource (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:5308)
00:56:27.264	▲  at iteratePitchingLoaders (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:4667)
00:56:27.264	▲  at runLoaders (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:8590)
00:56:27.265	▲  at NormalModule._doBuild (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:399612)
00:56:27.265	▲  at NormalModule.build (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:401640)
00:56:27.265	▲  at /opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:82059
00:56:27.265	▲  
00:56:27.265	▲  Import trace for requested module:
00:56:27.265	▲  cloudflare:sockets
00:56:27.265	▲  ./node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/cf/polyfills.js
00:56:27.266	▲  ./node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/cf/src/index.js
00:56:27.266	▲  ./src/utils/db.ts
00:56:27.266	▲  ./src/app/api/users/route.ts
00:56:27.266	▲  ./node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fusers%2Froute&page=%2Fapi%2Fusers%2Froute&pagePath=private-next-app-dir%2Fapi%2Fusers%2Froute.ts&appDir=%2Fopt%2Fbuildhome%2Frepo%2Fsrc%2Fapp&appPaths=%2Fapi%2Fusers%2Froute&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!./src/app/api/users/route.ts?__next_edge_ssr_entry__
00:56:27.266	▲  
00:56:27.266	▲  node:stream
00:56:27.266	▲  Module build failed: UnhandledSchemeError: Reading from "node:stream" is not handled by plugins (Unhandled scheme).
00:56:27.267	▲  Webpack supports "data:" and "file:" URIs by default.
00:56:27.267	▲  You may need an additional plugin to handle "node:" URIs.
00:56:27.267	▲  at /opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:399825
00:56:27.267	▲  at Hook.eval [as callAsync] (eval at create (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:13:28867), <anonymous>:6:1)
00:56:27.267	▲  at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:13:26021)
00:56:27.267	▲  at Object.processResource (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:399750)
00:56:27.268	▲  at processResource (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:5308)
00:56:27.268	▲  at iteratePitchingLoaders (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:4667)
00:56:27.268	▲  at runLoaders (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:8590)
00:56:27.268	▲  at NormalModule._doBuild (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:399612)
00:56:27.268	▲  at NormalModule.build (/opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:401640)
00:56:27.269	▲  at /opt/buildhome/repo/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/webpack/bundle5.js:28:82059
00:56:27.269	▲  
00:56:27.269	▲  Import trace for requested module:
00:56:27.269	▲  node:stream
00:56:27.269	▲  ./node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/cf/src/large.js
00:56:27.269	▲  ./node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/cf/src/index.js
00:56:27.270	▲  ./src/utils/db.ts
00:56:27.270	▲  ./src/app/api/users/route.ts
00:56:27.270	▲  ./node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fusers%2Froute&page=%2Fapi%2Fusers%2Froute&pagePath=private-next-app-dir%2Fapi%2Fusers%2Froute.ts&appDir=%2Fopt%2Fbuildhome%2Frepo%2Fsrc%2Fapp&appPaths=%2Fapi%2Fusers%2Froute&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!./src/app/api/users/route.ts?__next_edge_ssr_entry__
00:56:27.270	▲  
00:56:27.270	▲  > Build failed because of webpack errors
00:56:27.299	▲  ELIFECYCLE  Command failed with exit code 1.
00:56:27.337	▲  Error: Command "pnpm run build" exited with 1
00:56:27.434	
00:56:27.434	⚡️ The Vercel build (`npx vercel build`) command failed. For more details see the Vercel logs above.
00:56:27.435	⚡️ If you need help solving the issue, refer to the Vercel or Next.js documentation or their repositories.
00:56:27.435	
00:56:27.483	Failed: Error while executing user command. Exited with error code: 1
00:56:27.495	Failed: build command exited with code: 1
00:56:28.663	Failed: error occurred while running build command


Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the import trace in src/utils/db.ts and src/app/api/users/route.ts, then inspect the postgres@3.4.3 Cloudflare files named in the build log. Reproduce the failure with pnpm run build and verify that the Next.js edge route builds successfully for the reported deployment target.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js, postgres, postgresql
Domain
backend, cloud, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.