Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node stream polyfill not working #1915

Closed
NoelDeMartin opened this issue Feb 6, 2021 · 3 comments
Closed

Node stream polyfill not working #1915

NoelDeMartin opened this issue Feb 6, 2021 · 3 comments

Comments

@NoelDeMartin
Copy link

Describe the bug

I've been trying to use a 3rd party library (jsonld-streaming-parser) with Vite and I haven't been able to make it work. I think the problem is that it's using node's stream API which is missing in the browser, so I've tried to replace it with stream-browserify to no avail (It may be related with browserify/stream-browserify#25, I'm not sure).

With webpack v4 it works out of the box, and I think that's because webpack bundles a stream polyfill by default.

Reproduction

Here's a reproduction of the issue in a small repo (only using Vite and the 3rd party library): https://github.com/NoelDeMartin/vite-sandbox

In case it's useful, here's the same code using webpack to bundle the library and writing the same code in Vite, which works as expected: https://github.com/NoelDeMartin/vite-sandbox/tree/webpack-bundle

System Info

  • vite version: vite/2.0.0-beta.65 linux-x64 node-v12.20.0
  • Operating System: Ubuntu 18.04.5 LTS
  • Node version: v12.20.0
  • Package manager (npm/yarn/pnpm) and version: npm 6.14.8

Logs (Optional if provided reproduction)

The error happens in the browser, serving doesn't throw any errors. It throws TypeError: Stream is undefined on this line.

Note: It also failed trying to use global and I fixed it by running window.global = window before importing the library.

@yyx990803
Copy link
Member

In general Vite does not try to support libraries that explicitly expect Node built-ins to be available in the browser. i.e. making Node libs run in the browser is explicitly a non-goal.

The lib you are using likely depends more than just a Stream shim, but as said, you'll have to figure that out yourself or stick to webpack.

NoelDeMartin added a commit to NoelDeMartin/soukai-solid that referenced this issue Mar 5, 2021
One of the main motivations for doing this change is that jsonld-streaming libraries don't work properly with Vite. More info: vitejs/vite#1915
@pkit
Copy link

pkit commented Mar 18, 2021

@yyx990803 just FYI, webpack will not work either. Because it will use the same stream-browserify internally (or externally in webpack@5)

@NoelDeMartin
Copy link
Author

@pkit I don't know exactly why, but webpack 4 worked for me on the repository I linked in my original text. I haven't tried with webpack 5. My theory was that webpack used an internal polyfill, but if you tell me it's using the same one then I have no idea what's going on 🙃.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants