davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
301 stars 22 forks source link

Uncaught ReferenceError: Buffer is not defined #22

Closed navan101 closed 1 year ago

navan101 commented 1 year ago

Hi @davidmyersdev,

  1. When i use vite-plugin-node-polyfills and config default, run dev environment it gives error buffer.
Screen Shot 2023-06-20 at 16 22 15

Screen Shot 2023-06-20 at 16 22 32

  1. When i run the build environment it works.

Screen Shot 2023-06-20 at 16 23 22

link: [https://codesandbox.io/p/github/navan101/vite-buffer/master?file=/src/App.vue:1,1&workspaceId=89d4ce07-40e9-48ee-965a-5e9183e6320b]

Thank you!

davidmyersdev commented 1 year ago

@navan101 this is fixed in v0.11.0.

rfe-sdev commented 1 year ago

Hi @davidmyersdev,

I seem to have the same kind of problem, even with version 0.11.0.

Capture d’écran 2023-08-08 à 16 55 58

Capture d’écran 2023-08-08 à 16 56 40

require('buffer') gives undefined so kMaxLength.toString(16) is impossible.

I'm in the same situation as @navan101, I have the default configuration and the build passes but the dev environment gives me this error.

Is this something you can fix on your end?

VysockyStan commented 1 year ago

Hi @davidmyersdev,

I seem to have the same kind of problem, even with version 0.11.0.

Capture d’écran 2023-08-08 à 16 55 58

Capture d’écran 2023-08-08 à 16 56 40

require('buffer') gives undefined so kMaxLength.toString(16) is impossible.

I'm in the same situation as @navan101, I have the default configuration and the build passes but the dev environment gives me this error.

Is this something you can fix on your end?

I have the same issue

davidmyersdev commented 1 year ago

@rfe-sdev @VysockyStan @navan101 can any of you confirm whether this is working properly in v0.11.1? Also, are you all using require rather than import? This could be an issue with the CommonJS build if so.

JounQin commented 1 year ago
image image

Same issue here with "vite-plugin-node-polyfills": "^0.11.1"

VysockyStan commented 1 year ago

@davidmyersdev I've tried 0.11.1. The issue persists. It occurs in 3rd-party lib which which uses browserify-zlib as on the screen that @rfe-sdev posted above. As you may see it uses require. So there is no chance to make it work with CommonJS ?

VysockyStan commented 1 year ago

But the difference that in my case Buffer is not undefined, however kMaxLength is

aryzing commented 1 year ago

I may have opened a dupe of this issue, https://github.com/davidmyersdev/vite-plugin-node-polyfills/issues/34. I also created a reproduction repo to help with debugging.

VysockyStan commented 1 year ago

I'm using https://www.npmjs.com/package/@ringcentral/sdk in React, which produces the error

aryzing commented 1 year ago

I've noticed that when removing the import that needs Buffer, the app does run fine, and Buffer is defined in the browser's console. This leads me to believe that perhaps there's something about the plugin that causes the polyfills to be loaded too late?

davidmyersdev commented 1 year ago

@aryzing that does appear to be the case. I think the code that is erroring is actually a side effect that runs during the import process (before the polyfill code gets a chance to run). I'm trying to figure out a way to get this working though.

davidmyersdev commented 1 year ago

I was able to verify that both reproductions (thank you to @navan101 and @aryzing for providing them) have been fixed in v0.11.2.

aryzing commented 1 year ago

So some good news, and some bad news. Good news: for smaller projects, v0.11.2 does work. The bad news is that for a larger project I'm working on, this plugin is causing the build to fail with a heap limit error,

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
Stack trace ``` 1: 0xc98550 node::Abort() [/home/user/.nvm/versions/node/v20.5.1/bin/node] 2: 0xb700a7 [/home/user/.nvm/versions/node/v20.5.1/bin/node] 3: 0xebae80 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/home/user/.nvm/versions/node/v20.5.1/bin/node] 4: 0xebb167 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/home/user/.nvm/versions/node/v20.5.1/bin/node] 5: 0x10cc7d5 [/home/user/.nvm/versions/node/v20.5.1/bin/node] 6: 0x10e4658 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/home/user/.nvm/versions/node/v20.5.1/bin/node] 7: 0x10ba771 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/home/user/.nvm/versions/node/v20.5.1/bin/node] 8: 0x10bb905 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/home/user/.nvm/versions/node/v20.5.1/bin/node] 9: 0x1098e76 v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/home/user/.nvm/versions/node/v20.5.1/bin/node] 10: 0x14f3ca6 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/home/user/.nvm/versions/node/v20.5.1/bin/node] 11: 0x7f93f7ed9ef6 Aborted (core dumped) ```

Possibly useful info:

I've created a new reproduction repo using this plugin on a large codebase which displays the error above during the build.

davidmyersdev commented 1 year ago

@aryzing would you mind opening a new issue for this?