nodejs_compat compatibility-flags flags not working for Pages functions

I've added nodejs_compat to both prod and preview in the UI but every deploy fails with these errors Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility. I can run it fine locally using --node-compat
4 Replies
RyanD
RyanDOP15mo ago
locally, if I don't include node-compat I get the same error as when I deploy it. It feels the flag is not working when deploying I'm trying to just use pg like in one of the examples
10:16:15.088 ✘ [ERROR] Build failed with 24 errors:
10:16:15.088
10:16:15.088 ../node_modules/pg-cloudflare/dist/index.js:1:29: ERROR: Could not resolve "events"
10:16:15.088 ../node_modules/pg-connection-string/index.js:76:77: ERROR: Could not resolve "fs"
10:16:15.088 ../node_modules/pg-pool/index.js:2:29: ERROR: Could not resolve "events"
10:16:15.089 ../node_modules/pg-protocol/dist/parser.js:9:41: ERROR: Could not resolve "assert"
10:16:15.089 ../node_modules/pg/lib/client.js:3:27: ERROR: Could not resolve "events"
10:16:15.089 ...
10:16:15.089
10:16:15.088 ✘ [ERROR] Build failed with 24 errors:
10:16:15.088
10:16:15.088 ../node_modules/pg-cloudflare/dist/index.js:1:29: ERROR: Could not resolve "events"
10:16:15.088 ../node_modules/pg-connection-string/index.js:76:77: ERROR: Could not resolve "fs"
10:16:15.088 ../node_modules/pg-pool/index.js:2:29: ERROR: Could not resolve "events"
10:16:15.089 ../node_modules/pg-protocol/dist/parser.js:9:41: ERROR: Could not resolve "assert"
10:16:15.089 ../node_modules/pg/lib/client.js:3:27: ERROR: Could not resolve "events"
10:16:15.089 ...
10:16:15.089
that's the error when I deploy, when it tries to build
Victor
Victor15mo ago
nodejs_compat is runtime apis only. They're not built into your code, but simply resolve at runtime execution. The key takeaway here is:
Node.js APIs are available under the node: prefix, and this prefix must be used when importing modules, both in your code and the npm packages you depend on.
https://developers.cloudflare.com/workers/runtime-apis/nodejs --node-compat uses polyfill to recreate those functions. These get bundled into your code but that makes your code a bit bigger
Node.js compatibility · Cloudflare Workers docs
Most Workers import one or more packages of JavaScript or TypeScript code from npm as dependencies in package.json. Many of these packages rely on …
Victor
Victor15mo ago
Based on your logs, it's looking for the node imports without the node: prefix
RyanD
RyanDOP14mo ago
so we can't use postgres from functions? I'm still confused why I can use --node-compat locally to get it to work but not when i deploy
Want results from more Discord servers?
Add your server