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)
[ERROR] Could not resolve "stream"
...
08:40:21.555 The package "stream" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
08:40:21.556
08:40:21.557 ✘ [ERROR] Could not resolve "stream"
08:40:21.557
08:40:21.557 node_modules/through/index.js:1:21:
08:40:21.557 1 │ var Stream = require('stream')
08:40:21.557 ╵
[ERROR] Could not resolve "stream"
...
08:40:21.555 The package "stream" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
08:40:21.556
08:40:21.557 ✘ [ERROR] Could not resolve "stream"
08:40:21.557
08:40:21.557 node_modules/through/index.js:1:21:
08:40:21.557 1 │ var Stream = require('stream')
08:40:21.557 ╵
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:
npm install --save stream-browserify
npm install --save stream-browserify
Then, update your vite.config.js file to include the following:
import { defineConfig } from 'vite'

export default defineConfig({
// other config
resolve: {
alias: {
stream: 'stream-browserify'
}
}
})
import { defineConfig } from 'vite'

export default defineConfig({
// other config
resolve: {
alias: {
stream: 'stream-browserify'
}
}
})
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
kian
kian8mo ago
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.
rawwerks
rawwerks8mo ago
so something like this?
export default defineConfig({
// other config
resolve: {
alias: {
stream: 'node:stream'
}
}
})
export default defineConfig({
// other config
resolve: {
alias: {
stream: 'node:stream'
}
}
})
kian
kian8mo ago
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.
rawwerks
rawwerks8mo ago
ok . what about adding https://www.npmjs.com/package/stream to my package.json? i fixed buffer this way...
kian
kian8mo ago
Buffer is in nodejs_compat as well. Old polyfills aren't generally recommended versus the built-in ones from nodejs_compat
rawwerks
rawwerks8mo ago
ok so your guidance would be like this?
import {Readable,Transform} from 'node:stream';
import { Buffer } from 'node:buffer';
import {Readable,Transform} from 'node:stream';
import { Buffer } from 'node:buffer';
kian
kian8mo ago
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.
rawwerks
rawwerks8mo ago
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:
09:47:47.932 > Using @sveltejs/adapter-cloudflare
09:47:48.948 ✘ [ERROR] Could not resolve "node:stream"
09:47:48.948
09:47:48.949 .svelte-kit/output/server/entries/pages/_page.svelte.js:6:7:
09:47:48.949 6 │ import "node:stream";
09:47:48.949 ╵ ~~~~~~~~~~~~~
09:47:48.949
09:47:48.949 The package "node:stream" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
09:47:48.949
09:47:48.949 ✘ [ERROR] Could not resolve "node:buffer"
09:47:47.932 > Using @sveltejs/adapter-cloudflare
09:47:48.948 ✘ [ERROR] Could not resolve "node:stream"
09:47:48.948
09:47:48.949 .svelte-kit/output/server/entries/pages/_page.svelte.js:6:7:
09:47:48.949 6 │ import "node:stream";
09:47:48.949 ╵ ~~~~~~~~~~~~~
09:47:48.949
09:47:48.949 The package "node:stream" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
09:47:48.949
09:47:48.949 ✘ [ERROR] Could not resolve "node:buffer"
kian
kian8mo ago
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)