A repro for using postgres.js with opennextjs.
The issue is opennextjs/opennextjs-cloudflare#1036
If you use follow the doc https://opennext.js.org/cloudflare/howtos/workerd
and add serverExternalPackages: ["postgres"], to your next.config.ts
You will get the following error when doing opennextjs-cloudflare build:
✘ [ERROR] Build failed with 1 error:
✘ [ERROR] The symbol "clearImmediate" has already been
declared
.open-next/server-functions/default/handler.mjs:289:40384:
289 │ ...lete(id))}),id}function clearImmediate(id){tasks.delete(id)}va...
╵ ~~~~~~~~~~~~~~
The symbol "clearImmediate" was originally declared here:
.open-next/server-functions/default/handler.mjs:1:76:
1 │ ...out, clearTimeout, setImmediate, clearImmediate} from "node:timers"
╵ ~~~~~~~~~~~~~~
clearImmediateis imported fromnode:timersby opennextjspostgres.jsdefine theclearImmediatefunction it the cloudflare polyfill.
banner: {
// We need to import them here, assigning them to `globalThis` does not work because node:timers use `globalThis` and thus create an infinite loop
// See https://github.com/cloudflare/workerd/blob/d6a764c/src/node/internal/internal_timers.ts#L56-L70
js: `import {setInterval, clearInterval, setTimeout, clearTimeout, setImmediate, clearImmediate} from "node:timers"`,
},
platform: "node",postgres.js define the clearImmediate function it the polyfill.
https://github.com/porsager/postgres/blob/master/cf/polyfills.js#L231-L233
function clearImmediate(id) {
tasks.delete(id)
}Right now, what's working for me is remove serverExternalPackages: ["postgres"], from my next.config.ts
The WRANGLER_BUILD_PLATFORM="node" from https://opennext.js.org/cloudflare/troubleshooting#my-app-fails-to-build-when-i-import-a-specific-npm-package does not work me ...