safe-buffer icon indicating copy to clipboard operation
safe-buffer copied to clipboard

safe-buffer in modern build tools like Vitejs

Open mecirmartin opened this issue 4 years ago • 10 comments

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

mecirmartin avatar Apr 19 '21 09:04 mecirmartin

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 avatar May 20 '21 20:05 phiiil

Adding buffer to node_modules fixed this for me.

npm i buffer

No need to change the safe-buffer package.

phiiil avatar May 21 '21 21:05 phiiil

I have the same problem in ViteJs

code-lixm avatar Jan 26 '22 02:01 code-lixm

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.

hatzz avatar Feb 02 '22 12:02 hatzz

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.

JasonGore avatar Feb 18 '22 00:02 JasonGore

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 :)

Scyks avatar Mar 19 '22 20:03 Scyks

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.

barbalex avatar May 07 '22 16:05 barbalex

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'

Oscar-Serna avatar Sep 29 '23 23:09 Oscar-Serna