safe-buffer
safe-buffer copied to clipboard
safe-buffer in modern build tools like Vitejs
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
If we included this simple check, this wouldn't error out in ViteJs environment.

Is there any reason not to include check like this? If you would be interested, i could submit PR
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.
Adding buffer to node_modules fixed this for me.
npm i buffer
No need to change the safe-buffer package.
I have the same problem in ViteJs
Adding buffer to node_modules fixed this for me.
npm i bufferNo need to change the safe-buffer package.
This worked even though i don't know why, thank you so much.
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.
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 :)
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.
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'