porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.09k stars 259 forks source link

Cloudflare Next.js Database error #781

Open fullyDeepak opened 6 months ago

fullyDeepak commented 6 months ago

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
pfurini commented 5 months ago

Same here, except I'm not even deploying on Cloudflare, it gives me the same error on my local machine when using the edge runtime (especially in middleware.ts when using NextJS 14 app router)

pfurini commented 5 months ago

UPDATE: it seems that when using postgres in an edge environment, the only supported runtime is the Cloudflare Workers one, it fails everywhere else. In the cf implementation it polyfills the Socket.connect() function with an explicit await import('cloudflare:sockets'). IMHO the edge implementation should be abstracted, for example using @arrowood.dev/socket to polyfill TCP sockets.