react-pdf: Fail to compile: Can't resolve 'stream'
I recently upgraded to webpack 5.65.0. @react-pdf/renderer@2.0.21 was working fine before I upgraded. Now ‘npm start’ gives me this error:
PS D:\Projects\NewEcommerce\EcommerceUI\ClientApp> npm start
> ecommerceui@0.1.0 start
> rimraf ./build && react-scripts start
Starting the development server...
Failed to compile.
Module not found: Error: Can't resolve 'stream' in 'D:\Projects\NewEcommerce\EcommerceUI\ClientApp\node_modules\@react-pdf\pdfkit\lib'
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: { "stream": require.resolve("stream-browserify") }'
- install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "stream": false }
[...]
Desktop:
- OS: Windows
- Browser firefox
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 29
- Comments: 33
Yes, this can be accomplished using the
cracopackage.Install
cracoand additional packages specified in the updatedreact-pdfdocs (thanks @jeetiss 🙏🏻):Change the
scriptssection inpackage.jsonas below:Finally, create a new file in the root of the project
craco.config.jswith these contents:This resolved the problem for me.
I’m wondering if is there any plans to fix the package instead of adding other packages as a workaround to make it work?
@jeetiss is there a solution for folks using unejected create-react-app? There’s no webpack file for me to modify. 😦
hey folks, I added setup instructions for webpack 5
https://github.com/diegomura/react-pdf#webpack-5
Could you please confirm that it helps?
Hello Folks!, For does of you who are using create-react-app and have installed the plugins as instructed Here. If you attempts to add the webpack config to your project root dir it will not work why? this because react js does not read that file unless you want to write an override file to webpack.
But never the less they is a work around to this all you have to do is edit the webpack configuration file that react-scripts uses in compiling the react app. with the configuration provided below:
yarn add process browserify-zlib stream-browserify util buffer assertCopy the plugins code and as past it to react-scripts in your node_modules file located at:
node_modules\react-scripts\config\webpack.config.jsnow for the fallback locate the resolve object and past the fallback provided here at the end.
***Note Make sure to install the files first before attempting the settings.
Hope this help cause it solve my problem.
add
"stream": "npm:stream-browserify@^3.0.0",to your package.json ornpm i stream@npm:stream-browserifyI don’t think this is an error that
react-pdfis throwing, it’s an error thatwebpackis throwing, due to webpack v5 having removed node.js polyfills: https://webpack.js.org/blog/2020-10-10-webpack-5-release/#automatic-nodejs-polyfills-removed.Thus for people using
create-react-app, since it uses webpack, you will get this error unless you apply one of the solutions likecracoorreact-app-rewired.Here’s the issue for
create-react-app; it contains additional information and opinions from developers: https://github.com/facebook/create-react-app/issues/11756I’m thinking about trying a newer build tool like Vite which does the work of webpack and includes quick-start templates like CRA:
yarn create vite my-react-app --template react-ts@gmartinu I am not have any luck implementing you fix for Webpack 5.
I have only installed the npms and added your recommendations to my webpack.config.js. When I try running I get this:
I have all the reactpdf references commented out in my code, I am just trying to get the additions loaded properly. I have attached a zip of my webpack.config.js.
webpack.config.zip
Getting a Invalid configuration object when running with the craco solution for CRA. Any ideas?
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
Its almost 9 months and no fix released yet on npm ? 9 months are very long for this library which is used by millions.
craco based solution works but it should have been stop-gap solution.
With craco and the configuration above I’m getting this error:
how to solve it?
hey folks, for those who still have “Module not found: Error: Can’t resolve ‘stream’” error (stream and zlib) even after installing the shims and configuring them. It’s probably because you set up aliases in your Webpack config, I fixed the problem by adding aliases for stream and zlib
Great answer, worked like a charm
@RomanTurner Hi Roman, the only thing in the error that stands out to me is
configuration.resolve. In mycraco.config.js, the object that containsresolveisconfigure.I posted a start to finish example using CRA and react-pdf to stackoverflow: https://stackoverflow.com/a/70441023/2193573
I’d offer that if you follow that and get to a successful build, perhaps you can compare the config with that of your project and see what might be wrong.
Good luck!