ketting icon indicating copy to clipboard operation
ketting copied to clipboard

`Cannot access "stream.Stream" in client code` when using ketting w/ ViteJS

Open jamesdh opened this issue 1 year ago • 2 comments

I recently converted one of our web apps from Create React App to Vite. Now in the process of replacing some legacy code w/ ketting. Ketting worked fine in another CRA app of ours, but when attempting to use the latest (8.0.0-alpha.2) w/ Vite, I get the following on startup:

Module "stream" has been externalized for browser compatibility. Cannot access "stream.Stream" in client code. See http://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
get @ index.js:15
(anonymous) @ sax.js:20
node_modules/sax/lib/sax.js @ sax.js:1414
__require2 @ chunk-TFWDKVI3.js?v=4593b7b9:18
node_modules/ketting/dist/util/html.js @ sax.js:1424
__require2 @ chunk-TFWDKVI3.js?v=4593b7b9:18
node_modules/ketting/dist/state/html.js @ sax.js:1520
__require2 @ chunk-TFWDKVI3.js?v=4593b7b9:18
node_modules/ketting/dist/state/index.js @ html.ts:82
__require2 @ chunk-TFWDKVI3.js?v=4593b7b9:18
(anonymous) @ html.ts:93

Per Vite:

When you use a Node.js module in the browser, Vite will output the following warning.

Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFile" in client code. This is because Vite does not automatically polyfill Node.js modules.

We recommend avoiding Node.js modules for browser code to reduce the bundle size, although you can add polyfills manually. If the module is imported from a third-party library (that's meant to be used in the browser), it's advised to report the issue to the respective library.

I was able to simply yarn add stream and get around this. I don't fully understand the issue or Vite's suggestion, but I thought I'd raise this nonetheless.

jamesdh avatar Apr 13 '23 17:04 jamesdh