简体   繁体   中英

Buffer is not defined in react-pdf

I try to use the react-pdf library to generate pdf.

But when I do this I have an error like this:

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: { "assert": require.resolve("assert/") }' - install 'assert' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "assert": false } resolve 'assert' in 'C:\Users\SMAX\OneDrive\Projets Autres\exxotax_react\node_modules\browserify-zlib\lib'

Now, after installing some packages the React/Webpack is compiled from npm run start. But I have the following error:

Buffer is not defined And nothing appear on screen.

How can I fix it? I tried:

  • Install all libraries needed
  • Watch on internet some solution

The only solution I saw was something like:

Creating a webpack.config.js and add:

resolve: {
extensions: [ '.ts', '.js' ],
fallback: {
    "stream": require.resolve("stream-browserify"),
    "buffer": require.resolve("buffer")
} },

But how can I add these lines in the config file? Do I need a new config file/package.json? If yes how can I do?

To extend create-react-app webpack config, you will need to use craco or rewired instead of react-scripts

Craco: https://github.com/gsoft-inc/craco

Rewired: https://github.com/timarney/react-app-rewired

I finally updated all packages and use a workaorund because of a "conflict" with webpack.

I have this issue but I dont want to "rewire" any configuration. Any alternative for this matter?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM