node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

CloudflareSocket is not a constructor in [email protected]

Open astanciu opened this issue 6 months ago • 3 comments

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?

astanciu avatar Jun 18 '25 19:06 astanciu

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

TrySound avatar Jun 19 '25 06:06 TrySound

Reported https://github.com/cloudflare/workers-sdk/issues/9668

TrySound avatar Jun 19 '25 06:06 TrySound

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

korywka avatar Jun 19 '25 12:06 korywka

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"
     }
   },

TrySound avatar Jun 19 '25 15:06 TrySound

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?

PabloG6 avatar Jun 23 '25 14:06 PabloG6

Using patch-package for npm

Pnpm has builtin command for this

TrySound avatar Jun 23 '25 22:06 TrySound

@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 avatar Jun 26 '25 01:06 hyusetiawan

@hyusetiawan you can try fix from my comment: https://github.com/brianc/node-postgres/issues/3493#issuecomment-2987974927 it helped me at least :)

korywka avatar Jun 26 '25 07:06 korywka