`Error: Dynamic require of node:path is not supported` causes runtime error - how to troubleshoot?

My SvelteKit project on CF Pages builds successfully and has been great. All routes still work, except for a POST route, that gives this error. - I do not have node:path in my code (!) - I have nodejs_compat enabled (screenshot) - I removed deps and simplified this route Cloudflare does not give a stack track, frustratingly, so I don't know how to troubleshoot further. Any tips for how to investigate further?
No description
No description
6 Replies
James
James5mo ago
The best way to debug things like this is locally, if you can reproduce it there, via wrangler pages dev
jason
jasonOP5mo ago
TBH, never figured out how to set up a CF local env for SvelteKit yet...one day. I think I may have found it! I was able to use:
bun run build
wrangler pages dev
bun run build
wrangler pages dev
Then hit the problematic route to see the stack trace. Stack trace implies it's due to mime-types package I am using to convert a mime type into a file extension. https://www.npmjs.com/package/mime-types But now I have a way to keep researching. Thanks @James Do you know if wrangler pages dev can be used with hot module reloading? I can't image waiting 30s for a build to try each change, after being used to vite dev. .... Update: it has --live-reload but since it runs SvelteKit's production build output, you have to fully rebuild before it reloads.
lj
lj5mo ago
I found an example that npx wrangler pages dev -- npx react-scripts start but can't use with vite live reload. wrangler watch the sveltekit live reload but how to proxy the vite live result output?
jason
jasonOP4mo ago
For SvelteKit, I have two terminals 1) npx wrangler dev (and all flags I need for D1, R2, etc). 2.) bun run build. Then I have to run “bun run build” after making any file changes and saving, bc it had to rebuild the HTML files that wrangler is watching. I don’t know about React-related aspects
pneutam
pneutam4mo ago
I'm hitting this too @Jason with the CF Pages/SvelteKit combo. To state the obvious... it looks like it's because a dependency is found that imports a node library like path that is not compatible with the non-Node CF engine which is in force when running via wrangler pages dev. I only see these errors on re-loading a route and not navigating from the root URL, so I've suspected this is SSR related since first seeing this. Running code more on the client is one way to get around this. The nuclear option is setting ssr = false and run your SvelteKit app as an SPA. I want to keep SSR and I had some components I wanted to dynamically load on the client anyways (tuning bundles) and doing so made the problem go away. It's arisen again (why I read this thread) and looking more closely at the stack trace I can see my error is related to postcss and postcss-import in particular, which I can see does import "path". So my theory is that SvelteKit (or related dev plugins) use path in SSR rendering which is what blows up the route. I saw there was an [alias] option in wrangler.toml and I've been using pathe as an alternative, however CF is complaining that it's not a valid pages configuration so I am still looking for a fix.
jason
jasonOP4mo ago
For my case, using wrangler dev exposed the full error message which let me identify it was the mime-types package that uses node's path package (https://github.com/jshttp/mime-types/issues/82), and switching to the NPM mime package solves it--I just wrote my own minimal, mime validation function instead. I'd be dubious about SSR causing it b/c 1.) I used SSR on CF Pages previously successfully, and 2.) it's a key feature to support for each SvelteKit adapter (would be a big bug).
@Ben Does your stack trace from wrangler dev show the parent file that invokes the path module?
Want results from more Discord servers?
Add your server