Skip to content

webpack 5 issue: buffer fallback import crashes webpack compilation #448

Closed

Description

@devkral

Without fallback entry (require("buffer") or false) I get with webpack 5 the error:

ERROR in ./node_modules/safe-buffer/index.js 3:13-30
Module not found: Error: Can't resolve 'buffer' in '/.../node_modules/safe-buffer'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
- install 'buffer'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "buffer": false }
@ ./node_modules/string_decoder/lib/string_decoder.js 26:13-42
@ ./node_modules/readable-stream/lib/_stream_readable.js 163:40-80 325:38-78
@ ./node_modules/readable-stream/readable-browser.js 1:10-63

It would be nice if the import could be rewritten to not crash the webpack compilation.

Activity

  1. mcollina commented on Oct 25, 2020

    @mcollina
    SponsorMember

    I think might be better added to https://www.npmjs.com/package/safe-buffer.

  2. mcollina commented on Oct 25, 2020

    @mcollina
    SponsorMember
  3. feross commented on Oct 27, 2020

    @feross
    Contributor

    We need Node.js to continue using the built-in buffer which is provided by require('buffer') so I can't simple change that require to require('buffer/') without a massive performance hit.

    One possible solution: Leave the require('buffer') as-is so Node.js uses the built-in Buffer, but add https://www.npmjs.com/package/buffer as a dependency of safe-buffer so that webpack will find a package named buffer in node_modules.

    Can someone confirm if this approach works? If so, happy to apply the fix.

  4. Vinnl commented on Jul 5, 2021

    @Vinnl

    @feross Yes, I would expect that fix to work - currently, a workaround that works for us is to add buffer as a direct dependency, so ideally it would get pulled in automatically by safe-buffer. I think that would also resolve this issue: feross/safe-buffer#18 (Although the original issue wasn't described too clearly, after its closure they discuss running into this issue too and being able to resolve it by adding buffer as a dependency.)

    Edit: submitted a PR: feross/safe-buffer#33

  5. jimmywarting commented on Aug 31, 2021

    @jimmywarting
    Contributor

    should just stop depending on safe-buffer and switch to using buffer directly instead.
    safe-buffer isn't needed anymore

  6. mcollina commented on May 23, 2022

    @mcollina
    SponsorMember

    Will be fixed in v4.0.0

  7. jaxoncreed commented on Jul 6, 2022

    @jaxoncreed
    Contributor

    I might be missing something, but I'm still having problems with v4.0.0. On a clean install using react-create-app (it uses webpack v5), I get the following errors:

    ERROR in ./node_modules/readable-stream/lib/internal/streams/duplexify.js 5:21-38
    
    Module not found: Error: Can't resolve 'buffer' in '/Users/jackson/O/scratch/test-ldo-react/node_modules/readable-stream/lib/internal/streams'
    
    BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
    This is no longer the case. Verify if you need this module and configure a polyfill for it.
    
    If you want to include a polyfill, you need to:
    	- add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
    	- install 'buffer'
    If you don't want to include a polyfill, you can use an empty module like this:
    	resolve.fallback: { "buffer": false }
    
    
    ERROR in ./node_modules/readable-stream/lib/ours/util.js 3:21-38
    
    Module not found: Error: Can't resolve 'buffer' in '/Users/jackson/O/scratch/test-ldo-react/node_modules/readable-stream/lib/ours'
    
    BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
    This is no longer the case. Verify if you need this module and configure a polyfill for it.
    
    If you want to include a polyfill, you need to:
    	- add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
    	- install 'buffer'
    If you don't want to include a polyfill, you can use an empty module like this:
    	resolve.fallback: { "buffer": false }
    
    
    ERROR in ./node_modules/string_decoder/node_modules/safe-buffer/index.js 4:13-30
    
    Module not found: Error: Can't resolve 'buffer' in '/Users/jackson/O/scratch/test-ldo-react/node_modules/string_decoder/node_modules/safe-buffer'
    
    BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
    This is no longer the case. Verify if you need this module and configure a polyfill for it.
    
    If you want to include a polyfill, you need to:
    	- add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
    	- install 'buffer'
    If you don't want to include a polyfill, you can use an empty module like this:
    	resolve.fallback: { "buffer": false }
    

    It looks like buffer is still required in those files:
    https://github.com/nodejs/readable-stream/blob/main/lib/internal/streams/duplexify.js#L5
    https://github.com/nodejs/readable-stream/blob/main/lib/ours/util.js#L3
    https://github.com/feross/safe-buffer/blob/master/index.js#L4

  8. mcollina commented on Jul 6, 2022

    @mcollina
    SponsorMember

    Have you followed the docs in https://github.com/nodejs/readable-stream#usage-in-browsers? Could you open a fresh issue in case?

  9. jaxoncreed commented on Jul 6, 2022

    @jaxoncreed
    Contributor

    Ah, the goal was to require no webpack modifications. I guess I misunderstood the purpose of this library.

    Turns out simply installing buffer (npm i buffer) allows you to use this library without any webpack changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions