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

safe-buffer in modern build tools like Vitejs #30

Open
mecirmartin opened this issue Apr 19, 2021 · 8 comments
Open

safe-buffer in modern build tools like Vitejs #30

mecirmartin opened this issue Apr 19, 2021 · 8 comments

Comments

@mecirmartin
Copy link

Hey, i'm trying to use ViteJs for project.

Since this is es6 only, the code errors out inside safe-buffer package Buffer is undefined here

Screenshot 2021-04-19 at 11 31 26

If we included this simple check, this wouldn't error out in ViteJs environment.
Screenshot 2021-04-19 at 11 32 12

Is there any reason not to include check like this? If you would be interested, i could submit PR

@phiiil
Copy link

phiiil commented May 20, 2021

Hi @mecirmartin, I have the same issue in Sveltekit (which uses Vite). While trying to debug I noticed than bn also uses Buffer and the initialization is more involved and adapted to a bowser context.

  var Buffer;
  try {
    if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
      Buffer = window.Buffer;
    } else {
      Buffer = require('buffer').Buffer;
    }
  } catch (e) {
  }

Please submit your PR, I'm interested.

@phiiil
Copy link

phiiil commented May 21, 2021

Adding buffer to node_modules fixed this for me.

npm i buffer

No need to change the safe-buffer package.

@code-lixm
Copy link

I have the same problem in ViteJs

@hatzz
Copy link

hatzz commented Feb 2, 2022

Adding buffer to node_modules fixed this for me.

npm i buffer

No need to change the safe-buffer package.

This worked even though i don't know why, thank you so much.

@JasonGore
Copy link

JasonGore commented Feb 18, 2022

This should absolutely result in a change in safe-buffer package. buffer should be declared as a dep by safe-buffer in its package.json if it requires it. I found this problem when I noticed that our bundled output is using any random version of buffer it comes across installed by another dependency, regardless of buffer major versioning. This will lead to insidious and hard to troubleshoot and the dep should be declared by safe-buffer.

@Scyks
Copy link

Scyks commented Mar 19, 2022

I suddenly faced the same issue and the error occured during a code change. Everything was working before.
Somehow VSCode added import e from 'express'. this single line of code causes the issue.

Maybe that helps someone :)

@barbalex
Copy link

barbalex commented May 7, 2022

I installed react-xml-viewer. Then my app failed with errors mentioning safe-buffer. Following above comments I added buffer as dependency which solved the issue. Please declare this dependency.

@Oscar-Serna
Copy link

Oscar-Serna commented Sep 29, 2023

De repente me enfrenté al mismo problema y el error se produjo durante un cambio de código. Todo funcionaba antes. De alguna manera se agregó VSCode import e from 'express'. esta única línea de código causa el problema.

Quizás eso ayude a alguien :)

It helped me, thank you. For some reason { token } was imported from 'morgan'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants