use node `stream` or workaround for sveltekit in CF?
i'm using sveltekit with CF. i'm having an issue when i import the
stl
npm package with import stl from 'stl';
, i can run locally but it breaks on CloudFlare with this error (https://www.npmjs.com/package/stl)
the advice i'm getting from Cursor.sh (GPT-4) is below:
The error message indicates that the package stream is not found in your project's dependencies. The stream module is a built-in Node.js module, and it seems like your project is trying to bundle it for the browser, which is causing the error.
To fix this issue, you can use the resolve option in Vite's configuration to alias the stream module to a browser-compatible version. You can use the stream-browserify package for this.
First, install stream-browserify:
Then, update your vite.config.js file to include the following:
This will tell Vite to use stream-browserify whenever stream is imported, which should resolve the error.
---
this seems like an odd issue, maybe i just need to update node? is stream-browserify really necessary? (following these instructions still gives the same error)npm
stl
STL to Array of verts and vice versa. Latest version: 1.1.1, last published: 9 years ago. Start using stl in your project by running
npm i stl
. There are 11 other projects in the npm registry using stl.9 Replies
Workers aren't Node, and so don't have Node built-in modules.
A limited subset of modules are available via the
nodejs_compat
compatibility flag, https://developers.cloudflare.com/workers/runtime-apis/nodejs/, which includes node:stream
.
If you can tell Vite that stream
should load node:stream
, and enable that compatibility flag, it might work.
It's possible that the library wants more than just stream
though.so something like this?
Possibly, I haven't used Vite (or that option) myself but on paper it should work.
stream
would work in Node but Cloudflare's nodejs_compat
flag makes it available as node:stream
, not just stream
- so bundlers need to rewrite the imports.ok . what about adding
https://www.npmjs.com/package/stream
to my package.json?
i fixed buffer
this way...Buffer is in
nodejs_compat
as well.
Old polyfills aren't generally recommended versus the built-in ones from nodejs_compat
ok so your guidance would be like this?
Yes, for your own code - in the case of dependencies like
through
(via stl
) then you need to somehow tell that dependency to use node:stream
rather than stream
, which Vite should be able to do.ok thanks i'll dig in some more. i just added the flag to my pages instance and can build from there.
some work ahead of me:
Ah, yeah - Wrangler's esbuild config marks
node:
as external when using nodejs_compat
but Svelte's config doesn't (https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare/index.js#L61)