Open janpio opened 11 months ago
Hello is there any workaround for this issue? I try to use pg and prisma on cloudflare next-on-pages with nextjs application. Observe similar issue.
Hello is there any workaround for this issue? I try to use pg and prisma on cloudflare next-on-pages with nextjs application. Observe similar issue.
This build error comes from Next.js and is something we have no control over.
@janpio / @VanThanh12195 have you found a workaround?
@james-elicx can you provide tips what a developer can/should do fix it? next-on-pages allows to develop next inside wranger context, if its ok to call it like this. pg is supported by cloudflare, and those errors are coming from loading pg dependencies inside the wrangler context. I mean, you guys are probably dealing with it on the cloudflare side, let it make work on the dev side, or show us how, please.
@james-elicx can you provide tips what a developer can/should do fix it? next-on-pages allows to develop next inside wranger context, if its ok to call it like this. pg is supported by cloudflare, and those errors are coming from loading pg dependencies inside the wrangler context. I mean, you guys are probably dealing with it on the cloudflare side, let it make work on the dev side, or show us how, please.
Module not found: Can't resolve
is a Next.js build error because they throw errors when libraries import Node.js builtins that aren't supported according to their edge runtime implementation. It has nothing to do with us or how our dev mode works, and we can't stop it from happening.
Module not found: Can't resolve
is a Next.js build error because they throw errors when libraries import Node.js builtins that aren't supported according to their edge runtime implementation. It has nothing to do with us or how our dev mode works, and we can't stop it from happening.
@james-elicx I understand that the build error is coming from nextjs. However, lets focus on a possible solution or a workaround to use pg
in a next-on-pages
project.
Or just declare that next-on-pages
does not support pg
and call it a day..
Has anyone solved this problem?
There might be a way to make it work by configuring Next.js and patching pg
. Super hacky, but it shows there is a way forward: https://github.com/brianc/node-postgres/issues/3206#issuecomment-2154419602
Not working pg connection, MariaDB connection and MySQL connection in to middleware.ts How then use middleware.ts with db connections? May be use pg connection in layout dashboard for auth?
facing the same exact problem due to wanting to use cloudflare and vercel deployment with edge runtime.
Below are the summary i am facing,
import { Prisma, PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export const runtime = 'edge';
//schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = []
}
in a cloudflare preview deployed environment. If a page call an api that requires prisma, the following error occurs
Next, i replace it with prisma/adapter-pg in attempt to resolve Next edge runtime
import { Pool } from 'pg'
import { PrismaPg } from '@prisma/adapter-pg'
import { Prisma, 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 } as any)
export const runtime = 'edge';
//schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
wonder if anyone resolved this
If you're trying to get pg
working — it's worth experimenting with @opennextjs/cloudflare
, which lets you use a much wider set of Node.js APIs, which DB drivers like pg
typically depend on. (The edge "runtime" in the Next.js compiler intentionally constrains which Node.js APIs your app can use to a much narrower set than what Cloudflare Workers supports)
If you're trying to get
pg
working — it's worth experimenting with@opennextjs/cloudflare
, which lets you use a much wider set of Node.js APIs, which DB drivers likepg
typically depend on. (The edge "runtime" in the Next.js compiler intentionally constrains which Node.js APIs your app can use to a much narrower set than what Cloudflare Workers supports)
Has anybody yet been able to get the Cloudflare version of OpenNext to work with prisma?
The following prisma configuration works fine with next dev
, builds without errors with npx cloudflare
but fails when using wrangler dev
or wrangler deploy
:
import { Pool } from "pg";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "@prisma/client";
const pool = new Pool({ connectionString: connectionString, max: 1 })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })
Error messages:
Could not resolve "#main-entry-point"
.worker-next/.next/standalone/node_modules/.prisma/client/default.js:1:30:
1 │ module.exports = { ...require('#main-entry-point') }
╵ ~~~~~~~~~~~~~~~~~~~
The module "./wasm.js" was not found on the file system:
.worker-next/.next/standalone/node_modules/.prisma/client/package.json:108:19:
108 │ "workerd": "./wasm.js",
╵ ~~~~~~~~~~~
and
ould not resolve "pg-cloudflare"
.worker-next/.next/standalone/node_modules/pg/lib/stream.js:10:41:
10 │ 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.
Adding pg-cloudflare as a dependency does also not seem to help. Wondering if anybody has managed to set it up successfully.
having same issue as above ^
@maltekuehl were you able to figure this out?
@irvinebroque do you have an example of using @opennextjs/cloudflare with prisma to get it working?
Btw - not an expert by any means, but I had managed to get supabase (postgres) working with the neon driver from prisma, not sure if that's helpful, but might be worth a shot until pg is working?
@karthikjn01 could you give me an example of how you set it up?
Unfortunately don't have the code on hand. Have a look here: https://www.prisma.io/docs/orm/overview/databases/neon
@karthikjn01 i just tried it using the neon drivers and got this error
same issue as above. What does your package.json look like?
Ah yeah, I was running into that on opennext - try next-on-pages, I believe that's where it was working.
hi yes thank you @karthikjn01 i got it working with next-on-pages, thank you. It looks like it sstill bugged on opennext
I seem to have gotten it compiling and running with prisma pool, but ther seems to be approx 1 second overhead for each function call made, as I think the setting up of the PrismaClient with the adapter takes around a second each time to instantiate. @karthikjn01 have you had any of those issues? The moment i moved it back to a singleton (to test this theory) it worked fine
If you're trying to get
pg
working — it's worth experimenting with@opennextjs/cloudflare
, which lets you use a much wider set of Node.js APIs, which DB drivers likepg
typically depend on. (The edge "runtime" in the Next.js compiler intentionally constrains which Node.js APIs your app can use to a much narrower set than what Cloudflare Workers supports) https://opennext.js.org/cloudflareHas anybody yet been able to get the Cloudflare version of OpenNext to work with prisma?有人能够让 OpenNext 的 Cloudflare 版本与 prisma 一起使用吗?
The following prisma configuration works fine with
next dev
, builds without errors withnpx cloudflare
but fails when usingwrangler dev
orwrangler deploy
:以下 prisma 配置在next dev
中运行良好,在npx cloudflare
中构建时没有错误,但在使用wrangler dev
或wrangler deploy
时失败:import { Pool } from "pg"; import { PrismaPg } from "@prisma/adapter-pg"; import { PrismaClient } from "@prisma/client"; const pool = new Pool({ connectionString: connectionString, max: 1 }) const adapter = new PrismaPg(pool) const prisma = new PrismaClient({ adapter })
Error messages:
Could not resolve "#main-entry-point" .worker-next/.next/standalone/node_modules/.prisma/client/default.js:1:30: 1 │ module.exports = { ...require('#main-entry-point') } ╵ ~~~~~~~~~~~~~~~~~~~ The module "./wasm.js" was not found on the file system: .worker-next/.next/standalone/node_modules/.prisma/client/package.json:108:19: 108 │ "workerd": "./wasm.js", ╵ ~~~~~~~~~~~
and
ould not resolve "pg-cloudflare" .worker-next/.next/standalone/node_modules/pg/lib/stream.js:10:41: 10 │ 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.
Adding pg-cloudflare as a dependency does also not seem to help.添加 pg-cloudflare 作为依赖项似乎也没有帮助。 Wondering if anybody has managed to set it up successfully.想知道是否有人成功设置过。
I also encountered this problem and couldn't work together.
I seem to have gotten it compiling and running with prisma pool, but ther seems to be approx 1 second overhead for each function call made, as I think the setting up of the PrismaClient with the adapter takes around a second each time to instantiate. @karthikjn01 have you had any of those issues? The moment i moved it back to a singleton (to test this theory) it worked fine我似乎已经使用 prisma 池编译和运行它,但每次函数调用似乎大约需要 1 秒的开销,因为我认为使用适配器设置 PrismaClient 每次实例化大约需要一秒钟。 @karthikjn01你有遇到过这些问题吗?当我将它移回单例(以测试这个理论)时,它工作得很好
Could not resolve "#main-entry-point"
Have you solved this issue?
next-on-pages environment related information
Description
pg
has support for Cloudflare Workers and Pages. This project makes it possible to run Next.js projects on Cloudflare Pages.Unfortunately something gets lost along the way, so that
pg
triggers webpack errors duringbuild
anddev
:I theorize that the problem is because
pg
was made to use a adapter version of some code when running inworkerd
. But during the Next.js build this is not set, so it thinks it is running in a normal Node environment and includes the wrong code - which then would not work inedge-light
which the Next.js bundler assumes for edge functions - and hence fails.Reproduction
https://github.com/janpio/cloudflare-next-prisma/tree/plain-pg
Pages Deployment Method
Direct Upload (
wrangler pages publish
or the @cloudflare/pages-action GitHub Action)Pages Deployment ID
No response
Additional Information
No response
Would you like to help?