react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Error: Cannot find module './zlib_bindings'

Open Mohammed-Qasim93 opened this issue 3 years ago • 19 comments

hi guys

I have an error with my app it says

Error: Cannot find module './zlib_bindings'

my webpack config

Mohammed-Qasim93 avatar Jan 14 '22 11:01 Mohammed-Qasim93

I am having the same issue

nischalp444 avatar Jan 21 '22 10:01 nischalp444

Could you please provide more information about setup and error or create repo with minimal reproduction because this info is limited and I don't know how to help you?

jeetiss avatar Jan 21 '22 10:01 jeetiss

Yes, I will list the steps that I took to get to the issue that I am facing.

  1. Create a React Project
  2. yarn add @react-pdf/renderer

issues: - Module not found: Error: Can't resolve 'stream' in 'react- pdf\node_modules@react-pdf\pdfkit\lib' - Module not found: Error: Can't resolve 'zlib' in 'react- pdf\node_modules@react-pdf\pdfkit\lib' - Module not found: Error: Can't resolve 'zlib' in 'react- pdf\node_modules@react-pdf\png-js\lib' - Module not found: Error: Can't resolve 'stream' in 'react- pdf\node_modules\blob-stream' - Module not found: Error: Can't resolve 'stream' in 'react- pdf\node_modules\restructure\src'

  1. In order to fix these issues I tried doing the "yarn add process browserify-zlib stream-browserify util buffer assert", but the issues were still there.
  2. That was unsuccessful so I tried to add the following code to webpack.config located inside node_modules/react-scripts.
const webpack = require("webpack");

module.exports = {
  /* ... */

  resolve: {
    fallback: {
      process: require.resolve("process/browser"),
      zlib: require.resolve("browserify-zlib"),
      stream: require.resolve("stream-browserify"),
      util: require.resolve("util"),
      buffer: require.resolve("buffer"),
      asset: require.resolve("assert"),
    }
  },
  plugins: [
    new webpack.ProvidePlugin({
      Buffer: ["buffer", "Buffer"],
      process: "process/browser",
    }),
  ]

  /* ... */
}
  1. I went through the whole folder and made sure the placement was correct, but when I restarted the project it gave me a webpack error and I was unable to do a yarn start so I removed the changes that I made.
  2. I went installed each error that I was receiving with the following code:

npm i stream npm i zlib npm i stream

  1. this removed all the errors but the one that says "Error: Cannot find module './zlib_bindings"
  2. I tried attaching the file to the webpack again, but I am getting the same error.

Please let me know if you need more information.

nischalp444 avatar Jan 21 '22 15:01 nischalp444

The latest version of create react app (and really esm) require file extensions for all import paths. Don't know if this is related or not. seems more like random node imports being used on the front-end.

brandonryan avatar Jan 21 '22 17:01 brandonryan

I was able to get it to work by using an older version

nischalp444 avatar Jan 22 '22 10:01 nischalp444

That was unsuccessful so I tried to add the following code to webpack.config located inside node_modules/react-script

I think this is the reason. CRA doesn't provide a way to modify webpack config. You have to run inject or use some other tools that patch it for you.

here is grate guide on how to use react-pdf with CRA and craco tool https://dev.to/przpiw/react-pdf-rendering-4g7b

jeetiss avatar Jan 22 '22 12:01 jeetiss

I really don't think that makes sense... if its a library meant to be used in the browser, why does it depend on node libraries?

brandonryan avatar Jan 24 '22 07:01 brandonryan

I used the steps given from https://dev.to/przpiw/react-pdf-rendering-4g7b and it also worked. Thank you!

nischalp444 avatar Jan 25 '22 19:01 nischalp444

I used the steps given from https://dev.to/przpiw/react-pdf-rendering-4g7b and it also worked. Thank you! thank you sir , worked for me as well

Azamat-Shogen avatar May 07 '22 22:05 Azamat-Shogen

dont need the polyfill...i just renamed zlib/lib/zlib.js to zlib/lib/zlib_bindings.js and removed the code in the file

CramerJ1470 avatar May 17 '22 03:05 CramerJ1470

bare min working online sample in case helps anybody: https://stackblitz.com/edit/react-pdf-demo2?file=src%2Findex.js

Beej126 avatar May 25 '22 20:05 Beej126

I was able to get it to work by using an older version

Which version ? and how to install that kindly tell me.

faizansohail077 avatar May 31 '22 09:05 faizansohail077

@faizansohail077 - just wondering if you had trouble applying working sample on stackblitz?

Beej126 avatar May 31 '22 14:05 Beej126

I started from scratch and followed these steps and thankfully it was resolved. Very easy guide too: https://www.alchemy.com/blog/how-to-polyfill-node-core-modules-in-webpack-5

kimberleyrogers avatar Jul 24 '22 04:07 kimberleyrogers

ill try these changes in my code and get back you all ! thank you


From: Kimberley Rogers @.> Sent: Sunday, July 24, 2022 12:18 AM To: diegomura/react-pdf @.> Cc: john cramerec.com @.>; Comment @.> Subject: Re: [diegomura/react-pdf] Error: Cannot find module './zlib_bindings' (Issue #1678)

I started from scratch and followed these steps and thankfully it was resolved. Very easy guide too: https://www.alchemy.com/blog/how-to-polyfill-node-core-modules-in-webpack-5

— Reply to this email directly, view it on GitHubhttps://github.com/diegomura/react-pdf/issues/1678#issuecomment-1193243475, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASTDM3WNTNOD326OULY46GLVVS7X5ANCNFSM5L6RNFZA. You are receiving this because you commented.Message ID: @.***>

CramerJ1470 avatar Jul 25 '22 15:07 CramerJ1470

I started from scratch and followed these steps and thankfully it was resolved. Very easy guide too: https://www.alchemy.com/blog/how-to-polyfill-node-core-modules-in-webpack-5

Thank you! This solution works on my project!

CharleneKwok avatar Aug 05 '22 07:08 CharleneKwok

I have zlib and browserify-zlib in my devDependencies after I removed zlib everything working fine

chaiwattsw avatar Aug 08 '22 11:08 chaiwattsw

Anyone has a solution?

satinder1624 avatar Aug 08 '22 23:08 satinder1624

I just went in to node_modules/zlib/lib to zlib.js and commented out the code

CramerJ1470 avatar Aug 09 '22 04:08 CramerJ1470

Yes I am not being able to resolve this issue. Kindly guide me.how to resolved this.

On Tue, May 31, 2022, 7:15 PM Beej @.***> wrote:

@faizansohail077 https://github.com/faizansohail077 - just wondering if you had trouble applying working sample on stackblitz?

— Reply to this email directly, view it on GitHub https://github.com/diegomura/react-pdf/issues/1678#issuecomment-1142194783, or unsubscribe https://github.com/notifications/unsubscribe-auth/AML3YIUFY4HLAV4YS5VYPL3VMYNJPANCNFSM5L6RNFZA . You are receiving this because you were mentioned.Message ID: @.***>

faizansohail077 avatar Oct 11 '22 07:10 faizansohail077

i just checked and my stackblitz sample is still working; now also updated to latest @react-pdf/[email protected]. this sample project shows the exact bare minimum necessary. it is using craco to tweak webpack settings with aliases for zlib etc. make sure to compare your package.json to see what is required to be added to yours.

https://stackblitz.com/edit/react-pdf-demo2?file=craco.config.js

Beej126 avatar Oct 11 '22 14:10 Beej126

mine was within my react app so i changed my react-scripts version from 5.0.1 to 4.0.2 deleted node_modules and packagelock files and did a npm install . i highly recommend creating a duplicate of your entire directory first

CramerJ1470 avatar Nov 23 '22 07:11 CramerJ1470

What version of react-scripts are you using ?

CramerJ1470 avatar Nov 25 '22 00:11 CramerJ1470

I changed my react-scripts version to 4.0.2

CramerJ1470 avatar Nov 28 '22 21:11 CramerJ1470

mine was within my react app so i changed my react-scripts version from 5.0.1 to 4.0.2 deleted node_modules and packagelock files and did a npm install . i highly recommend creating a duplicate of your entire directory first

This solution worked for me... Thanks.