postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Cloudflare Next.js Database error

Open fullyDeepak opened this issue 2 years ago • 4 comments

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/[email protected][email protected][email protected]/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/[email protected][email protected][email protected]/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/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:13:26021)
00:56:27.264	▲  at Object.processResource (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:28:399750)
00:56:27.264	▲  at processResource (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:5308)
00:56:27.264	▲  at iteratePitchingLoaders (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:4667)
00:56:27.264	▲  at runLoaders (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:8590)
00:56:27.265	▲  at NormalModule._doBuild (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:28:399612)
00:56:27.265	▲  at NormalModule.build (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:28:401640)
00:56:27.265	▲  at /opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/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/[email protected]/node_modules/postgres/cf/polyfills.js
00:56:27.266	▲  ./node_modules/.pnpm/[email protected]/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/[email protected][email protected][email protected]/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/[email protected][email protected][email protected]/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/[email protected][email protected][email protected]/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/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:13:26021)
00:56:27.267	▲  at Object.processResource (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:28:399750)
00:56:27.268	▲  at processResource (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:5308)
00:56:27.268	▲  at iteratePitchingLoaders (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:4667)
00:56:27.268	▲  at runLoaders (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:8590)
00:56:27.268	▲  at NormalModule._doBuild (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:28:399612)
00:56:27.268	▲  at NormalModule.build (/opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/webpack/bundle5.js:28:401640)
00:56:27.269	▲  at /opt/buildhome/repo/node_modules/.pnpm/[email protected][email protected][email protected]/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/[email protected]/node_modules/postgres/cf/src/large.js
00:56:27.269	▲  ./node_modules/.pnpm/[email protected]/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/[email protected][email protected][email protected]/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


fullyDeepak avatar Jan 19 '24 19:01 fullyDeepak

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 avatar Feb 02 '24 10:02 pfurini

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.

pfurini avatar Feb 02 '24 11:02 pfurini