CloudflareSocket is not a constructor in [email protected]
I'm using pg in a cloudflare worker and this worker is using vite, using the cloudflare vite plugin.
The new pg-cloudflare package on version 1.2.6 is causing this error:
Unhandled error: TypeError: CloudflareSocket is not a constructor
at getStream2 (/Users/user/a/apps/a.com/node_modules/.vite/deps_a_test/pg.js:3090:16)
at new Connection2 (/Users/user/a/apps/a.com/node_modules/.vite/deps_a_test/pg.js:3136:40)
at new Client2 (/Users/user/a/apps/a.com/node_modules/.vite/deps_a_test/pg.js:3669:43)
at /Users/user/a/packages/worker/src/lib/service.ts:9:18
at dispatch (/Users/user/a/apps/a.com/node_modules/.vite/deps_a_test/chunk-7KAI6PC7.js:36:23)
at /Users/user/a/apps/a.com/node_modules/.vite/deps_a_test/chunk-7KAI6PC7.js:36:46
at AsyncContext.runWithContext (/Users/user/a/packages/utils/logger/context.ts:19:35)
at /Users/user/a/packages/worker/src/lib/requestPrep.ts:29:24
at dispatch (/Users/user/a/apps/a.com/node_modules/.vite/deps_a_test/chunk-7KAI6PC7.js:36:23)
at /Users/user/a/apps/a.com/node_modules/.vite/deps_a_test/chunk-7KAI6PC7.js:19:12
I dug around trying to figure it out, but was unable to. I saw in the pg-cloudflare readme references to things related to this.. I updated my worker's vite.config file with
build: {
rollupOptions: {
external: ["cloudflare:sockets"],
},
},
resolve: {
conditions: ["cloudflare"],
}
but this didn't help. Reverting back to 1.2.5 fixed me.
Am I missing some new required config or is there a bug somewhere?
This is because vite-plugin-cloudflare hardcodes export conditions and is using "workerd" instead of "cloudflare" https://github.com/cloudflare/workers-sdk/blob/main/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts#L128
Reported https://github.com/cloudflare/workers-sdk/issues/9668
any idea how to fix it temporary for pg? i downgraded "pg": "8.15.6" and "wrangler": "4.18.0" but anyway have this error :hiddenpain: i dont have vite-plugin-cloudflare in node_modules at all. but still have the same error
looks like this should fix it:
"pg": "8.15.6",
"pg-cloudflare": "1.2.5" // fix optional dep in pg
I fixed by patching pg-cloudflare like this
--- a/package.json
+++ b/package.json
@@ -15,6 +15,10 @@
"import": "./esm/index.mjs",
"require": "./dist/index.js"
},
+ "workerd": {
+ "import": "./esm/index.mjs",
+ "require": "./dist/index.js"
+ },
"default": "./dist/empty.js"
}
},
I fixed by patching pg-cloudflare like this
--- a/package.json +++ b/package.json @@ -15,6 +15,10 @@ "import": "./esm/index.mjs", "require": "./dist/index.js" },
"workerd": {"import": "./esm/index.mjs","require": "./dist/index.js" } },}, "default": "./dist/empty.js"
Not clear on how to implement this fix? could you provide a clearer solution?
@TrySound i am having this exact issue as well, do you have a suggestion that does not involve patching? Downgrading to a specific version maybe?
@hyusetiawan you can try fix from my comment: https://github.com/brianc/node-postgres/issues/3493#issuecomment-2987974927 it helped me at least :)