unable to deploy sveltekit when using postgres

I've been trying to deploy sveltekit using postgres, but am unable to get it to work with compatibility_flags = [ "nodejs_compat" ] or node_compat = true... I've made minimal repos showing it working with the default esbuild process wrangler does, while failing with sveltekid (which uses vite). Is anyone doing this something similar successfully? Any hints on how to tweak my vite.config.js to get it to work? Failing (sveltekit): https://terrible.software/terribleplan/svelte-kit-pg-cloudflare Working (no framework at all): https://terrible.software/terribleplan/raw-pg-cloudflare If you want to deploy the examples yourself make sure you update the wrangler.toml with a valid hyperdrive id. The errors I am getting from vite are of this variety (depending on how exactly I tweak my wrangler config to use compatibility_flags = [ "nodejs_compat" ] or node_compat = true):
✘ [ERROR] Could not resolve "node:path" (originally "path")

node_modules/pgpass/lib/index.js:3:19:
3 │ var path = require('path')
╵ ~~~~~~

The path "path" was remapped to "node:path" using the alias feature, which then couldn't be resolved. Keep in mind that import path aliases are resolved in the current working directory.

Cannot use "node:path" when deploying to Cloudflare.


✘ [ERROR] Could not resolve "fs"

node_modules/pgpass/lib/index.js:4:17:
4 │ , fs = require('fs')
╵ ~~~~

Cannot use "fs" when deploying to Cloudflare.
✘ [ERROR] Could not resolve "node:path" (originally "path")

node_modules/pgpass/lib/index.js:3:19:
3 │ var path = require('path')
╵ ~~~~~~

The path "path" was remapped to "node:path" using the alias feature, which then couldn't be resolved. Keep in mind that import path aliases are resolved in the current working directory.

Cannot use "node:path" when deploying to Cloudflare.


✘ [ERROR] Could not resolve "fs"

node_modules/pgpass/lib/index.js:4:17:
4 │ , fs = require('fs')
╵ ~~~~

Cannot use "fs" when deploying to Cloudflare.
Terrible Software
svelte-kit-pg-cloudflare
Terrible Software
raw-pg-cloudflare
8 Replies
Chaika
Chaika10mo ago
It is fairly confusing right now sadly, but Pages only supports wrangler.toml in dev. In prod, it ignores it entirely. Pages does not support hyperdrive or node_compat right now, only the nodejs compat flag. And pg only supports node_compat, not nodejs_compat node_compat -> polyfills in node stuff like path, only supported in Workers (or in pages if you custom build everything) nodejs_compat -> compat flag which adds in node:path and some popular node stuff in from the runtime, does not increase bundle size You could use a Worker and bind your Pages to it via a Service Binding
terribleplan
terribleplanOP10mo ago
As far as I can tell the issue isn't with the binding, and FWIW I am using workers:
import adapter from '@sveltejs/adapter-cloudflare-workers';

export default {
kit: {
adapter: adapter()
}
};
import adapter from '@sveltejs/adapter-cloudflare-workers';

export default {
kit: {
adapter: adapter()
}
};
Chaika
Chaika10mo ago
whoops somehow I had it in my head you were trying to use Pages, most people using Sveltekit do. pg only supports node_compat = true. What error do you get with that?
terribleplan
terribleplanOP10mo ago
Here are the full logs for node_compat = true
terribleplan
terribleplanOP10mo ago
As an interesting note compatibility_flags = [ "nodejs_compat" ] leads to fewer errors. All of the issues seem to be in the build stage, before the actual deploy to CF regardless.
Chaika
Chaika10mo ago
node_compat just
This is currently powered by @esbuild-plugins/node-globals-polyfill which in itself is powered by rollup-plugin-node-polyfills
you're not using wrangler's build process/you're using vite instead could try something like this: https://www.npmjs.com/package/vite-plugin-node-polyfills, not sure though, nothing I've tried before
terribleplan
terribleplanOP10mo ago
Yeah, vite is what svelte uses for its builds. Nothing changed when adding those polyfills, so it may be something with how the svelte plugin works. It isn't entirely clear what is producing those error messages, or why it is even when the node compat flag is there which should let me use (at least some of) the packages it is complaining about...
Chaika
Chaika10mo ago
it's vite. Normal wrangler deploy uses esbuild which uses that plugin when node_compat is enabled. You're not building through esbuild/normal wrangler though All that option really does is tell esbuild to include node-globals-polyfills when it bundles your code
Want results from more Discord servers?
Add your server