postgres orm drizzle setup can not be bundled by rollup

I've used the cloudflare pages template for qwik ( https://developers.cloudflare.com/pages/framework-guides/deploy-a-qwik-site/ ). Then following the guide for integrating hyperdrive I added postgres.js and drizzle . When trying to run the build script I get the following error
[commonjs--resolver] Cannot bundle Node.js built-in "os" imported from "node_modules/.pnpm/[email protected]/node_modules/postgres/src/index.js". Consider disabling ssr.noExternal or remove the built-in dependency.
[commonjs--resolver] Cannot bundle Node.js built-in "os" imported from "node_modules/.pnpm/[email protected]/node_modules/postgres/src/index.js". Consider disabling ssr.noExternal or remove the built-in dependency.
This library is recommended by the cloudflare docs ( https://developers.cloudflare.com/hyperdrive/learning/connect-to-postgres/ ) so I'm confused why it is giving errors. Anyone know how to fix this?
7 Replies
Barry
BarryOP15mo ago
I tried to override the build config with
build:{
rollupOptions:{
external:['os','fs','stream','net','tls','crypto']
}
},
build:{
rollupOptions:{
external:['os','fs','stream','net','tls','crypto']
}
},
which allows it to build, but that results in
✘ [ERROR] Build failed with 6 errors:

../server/assets/@qwik-city-plan-ad59c24d.js:1:186: ERROR: Could not resolve "os"
../server/assets/@qwik-city-plan-ad59c24d.js:1:205: ERROR: Could not resolve "fs"
../server/assets/@qwik-city-plan-ad59c24d.js:1:224: ERROR: Could not resolve "net"
../server/assets/@qwik-city-plan-ad59c24d.js:1:244: ERROR: Could not resolve "tls"
../server/assets/@qwik-city-plan-ad59c24d.js:1:264: ERROR: Could not resolve "crypto"
✘ [ERROR] Build failed with 6 errors:

../server/assets/@qwik-city-plan-ad59c24d.js:1:186: ERROR: Could not resolve "os"
../server/assets/@qwik-city-plan-ad59c24d.js:1:205: ERROR: Could not resolve "fs"
../server/assets/@qwik-city-plan-ad59c24d.js:1:224: ERROR: Could not resolve "net"
../server/assets/@qwik-city-plan-ad59c24d.js:1:244: ERROR: Could not resolve "tls"
../server/assets/@qwik-city-plan-ad59c24d.js:1:264: ERROR: Could not resolve "crypto"
when running pages:deploy I don't understand how in the cloudflare docs https://developers.cloudflare.com/workers/tutorials/postgres/ they just do import { Client } from "pg"; but don't explain how that handles the package's build-in node dependencies I found an old blog post https://blog.cloudflare.com/workers-tcp-socket-api-connect-databases/ about the tcp socket api that mentions you need to enable node_compat in the worker. but I can not create a wrangler.toml for pages, how am I supposed to enable node_compat for a pages app? Well I tried adding nodejs_compat to compatibility flags in the cloudflare console, but trying to deploy again results in the same error.
kian
kian15mo ago
I don't think you can for Pages, unfortunately. node_compat is different to nodejs_compat It's a set of polyfills added by an esbuild plugin
Barry
BarryOP15mo ago
How can cloudflare be promoting these features like HYPERDRIVE and have it only available for workers and not pages. Is it really so different? All these resources to get started on pages with so many frameworks and in the end they are useless because you don't get feature parity with plain workers. It is extremely frustrating trying to work with cloudflare... Thanks for your answer though, sorry for the rant.
kian
kian15mo ago
You can try add it into rollup yourself - https://github.com/ionic-team/rollup-plugin-node-polyfills/
GitHub
GitHub - ionic-team/rollup-plugin-node-polyfills
Contribute to ionic-team/rollup-plugin-node-polyfills development by creating an account on GitHub.
Barry
BarryOP15mo ago
It's giving issues but I don't think it will work regardless, I think hyperdrive relies on the node_compat to use the tcp socket api
kian
kian15mo ago
Hyperdrive itself doesn't, but the pg library does. node_compat is just @esbuild-plugins/node-globals-polyfill, which is just rollup-plugin-node-polyfills. What errors do you get after adding that plugin into rollup?
Barry
BarryOP15mo ago
I'm getting
Project not found. The specified project name does not match any of your existing projects. [code:
8000007]
Project not found. The specified project name does not match any of your existing projects. [code:
8000007]
instead of it asking what project to deploy to, I re-made the project a few times to test the compat modes, it's stuck on the old project name for some reason even though I have not hardcoded that anywhere does wrangler have any cache location? ah i got passed that by removing node_modules and re-installing. Okay it deployed successfully, giving an error because I have not configured the connection string environment variable yet... so let's try that Well.. managed to get the hyperdrive config created, but no way to bind it to a pages app, should have seen that coming by now
Want results from more Discord servers?
Add your server