opennextjs / opennextjs-cloudflare

Open Next.js adapter for Cloudflare
https://opennext.js.org/cloudflare
MIT License
378 stars 8 forks source link

Could not resolve "pg-cloudflare" when deploying to CloudFlare Workers #76

Closed gregavola closed 1 month ago

gregavola commented 1 month ago

I'm using Prisma as defined here, and I am able to run npm run dev have it working locally to serve content.

Once I run npm run build:worker && wrangler deploy - everything builds, but I get this error:

[ERROR] Could not resolve "pg-cloudflare"

.worker-next/.next/standalone/node_modules/pg/lib/stream.js:41:41:
  41 │     const { CloudflareSocket } = require('pg-cloudflare')
     ╵                                          ~~~~~~~~~~~~~~~

The module "./dist/index.js" was not found on the file system:

.worker-next/.next/standalone/node_modules/pg-cloudflare/package.json:13:15:
  13 │     "workerd": "./dist/index.js",
     ╵                ~~~~~~~~~~~~~~~~~

You can mark the path "pg-cloudflare" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

✘ [ERROR] Build failed with 2 errors:

.worker-next/.next/standalone/node_modules/.prisma/client/default.js:1:30: ERROR: Could not resolve "#main-entry-point" .worker-next/.next/standalone/node_modules/pg/lib/stream.js:41:41: ERROR: Could not resolve "pg-cloudflare"

import { Pool } from "pg";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "@prisma/client";

const connectionString = `${process.env.DATABASE_URL}`;

const pool = new Pool({ connectionString });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
export default prisma;
"dependencies": {
    "@aws-sdk/client-sqs": "^3.549.0",
    "@aws-sdk/util-utf8-node": "^3.259.0",
    "@prisma/adapter-pg": "^5.20.0",
    "@prisma/client": "^5.20.0",
    "dayjs": "^1.11.13",
    "next": "14.2.14",
    "pg": "^8.10.0",
    "prisma": "^5.20.0",
    "react": "^18",
    "react-dom": "^18",
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "@opennextjs/cloudflare": "^0.0.3",
    "@types/node": "^20.16.10",
    "@types/pg": "^8.11.10",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "eslint": "^8",
    "eslint-config-next": "14.2.14",
    "postcss": "^8",
    "tailwindcss": "^3.4.1",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "wrangler": "^3.80.1"
  }
vicb commented 1 month ago

Hey @gregavola could you please report your issue at https://github.com/cloudflare/workerd/discussions/categories/npm-package-requests instead.

It would be helpful to add a repo with the failing project.

Thanks

vicb commented 1 month ago

(I closed the report because this is related to worked rather than specific to next)