react-pdf: Cannot find module 'worker-loader!./build/pdf.worker.js'
hello,
is there anything i have to do besides import { Document, Page } from 'react-pdf/build/entry.webpack'
to use service workers? i’m running react-pdf 2.1.1 and nextjs (in case that’s of any relevance).
thank you! nicolai
index.js?d08fcf9:101 Cannot find module 'worker-loader!./build/pdf.worker.js'
Error: Cannot find module 'worker-loader!./build/pdf.worker.js'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> ([...]/node_modules/pdfjs-dist/webpack.js:18:19)
at Module._compile (module.js:569:30)
at Module._compile ([...]/node_modules/source-map-support/source-map-support.js:483:25)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 24 (5 by maintainers)
Not to hijack the issue but this issue seems to happen when running tests via Jest as well. We have a component that uses react-pdf, and any test that has this component fails for the same "Cannot find module ‘worker-loader!./build/pdf.worker.js’ from ‘webpack.js’ " error.
Do you know if there is something we need to setup to ensure testability? Definitely want the performance benefits but don’t want the lack of testability. If you need more detail let me know and I’ll do what I can!
Thanks!
EDIT: Turns out the easiest way to fix it was https://github.com/webpack-contrib/worker-loader/issues/10#issuecomment-325977537
@tim-phillips this applies to NextJS:
my
node_modules
include thepdfjs-dist
module which itself has thebuild/pdf.worker.js
file. this file is somehow not found byreact-pdf
when instantiated viaimport { Document } from 'react-pdf/dist/entry.webpack';
what i did was copy that file to
/static
and then instantiatereact-pdf
like so:i no longer get the error in production mode
hope this helps
RE: comment from @mgiraldo https://github.com/wojtekmaj/react-pdf/issues/67#issuecomment-384767613 on setting this up with next.js, things have changed a bit in version 4.x of react-pdf.
I still copied the worker file from
node_modules/pdfjs-dist/build/pdf.worker.min.js
into/static
. ButsetOptions
doesn’t seem to be a thing anymore. However, the following worked for me:Thanks so much @mgiraldo! I would not have figured this out without your comment.
@nnals did you figure out how to make it work with NextJS? i’m in the same problem
yes for create-react-app you need to map all worker files to some mock in your moduleMapper in package.json like so:
Obviously you should have a mock folder with workerMock.js in it, it could be something like this:
As a working solution you can:
webpack.config.base.js
addwebpack.config.test.js
addimport Document from 'react-pdf'
and it will use the version based on your current env.Just a small addition to @violabg comment, in order to succeed, you need to mock
default
static insideworkerMock.js
file as followingand allow create-react-app to override
moduleNameMapper
option by adding it into supportedKeys array insidecreateJestConfig.js
file.@halvard-cognite Is it publicly available? I think some people could be interested.
Anyone have a workaround for solving the same problem when running the tests with create-react-app where the webpack config is not directly available?
in package.json made the trick for us. But here is important that we import react-pdf in the code as
import { Document, Page } from 'react-pdf/dist/esm/entry.webpack';
hope that’ll help to someoneYou could also use moduleNameMapper to map
entry.webpack
toentry.jest
😃 That should also probably work.