-
-
Notifications
You must be signed in to change notification settings - Fork 848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot find module 'worker-loader!./build/pdf.worker.js' #67
Comments
Hey @nnals! I don't think you need to do anything else. Webpack should figure that one out if you have everything installed properly. Please make sure you have the |
could you explain do you think this might help? thank you! |
Oh man :) I do remember struggling with webpack at first a lot. I definitely feel you. First of all, I've worked super hard to make react-pdf very easy to install. It should not be necessary to change anything in your webpack configuration files. Perhaps you're using some non-compatible version of webpack? worker-loader for some reason accepts 2.x versions, but not yet 3.x versions. If that is not the case, the only thing I can recommend is to start from sample directory I've included and just try to get it as close as possible to your project, and see when it would fail. Sorry for the confusion in the previous post. |
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 webpack-contrib/worker-loader#10 (comment) |
As a working solution you can:
alias: { 'react-pdf': 'react-pdf/build/entry.webpack' }
alias: { 'react-pdf': 'react-pdf/build/entry.noworker' }
|
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? |
@halvard-cognite did you get the solution for this problem ? |
@SrikanthChebrolu No, we ended up maintaining a fork of react-scripts to be able to change stuff like this. |
@halvard-cognite Is it publicly available? I think some people could be interested. |
yes for create-react-app you need to map all worker files to some mock in your moduleMapper in package.json like so: {
"jest": {
"moduleNameMapper": {
"\\.worker.js":"<rootDir>/__mocks__/workerMock.js"
}
}
} Obviously you should have a mock folder with workerMock.js in it, it could be something like this: module.exports = Object.create(null); |
@nnals did you figure out how to make it work with NextJS? i'm in the same problem |
no, sorry. i switched to create-react-app shortly afterwards which also solved this problem.
…________________________________
From: Mauricio Giraldo <notifications@github.com>
Sent: Wednesday, April 25, 2018 5:23:54 PM
To: wojtekmaj/react-pdf
Cc: Nicolai Benker; Mention
Subject: Re: [wojtekmaj/react-pdf] Cannot find module 'worker-loader!./build/pdf.worker.js' (#67)
@nnals<https://github.com/nnals> did you figure out how to make it work with NextJS? i'm in the same problem
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#67 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AHEB7UmKmARBBTUxv2i_HRYx7SLPDp8xks5tsJUKgaJpZM4PjSl->.
|
i see. i ended up copying the not very elegant but works :) |
@mgiraldo how did you set this up? i'm confused by your description. |
@tim-phillips this applies to NextJS: my what i did was copy that file to import { setOptions, Document, Page } from "react-pdf";
setOptions({
workerSrc: "/static/pdf.worker.js"
}); i no longer get the error in production mode hope this helps |
@mgiraldo thank you! that works for me i used the minified worker at EDIT: I ended up not using this module due to the size and build times of PDF.js. I'm now simply linking to a url of the pdf and letting the browser do the work. |
Just a small addition to @violabg comment, in order to succeed, you need to mock
and allow create-react-app to override |
Cool, thx, but how do you override the createJestConfig.js? I don't have that in my project... |
CRA v.1* has this file under |
Found a solution for myself here. Instead of going via createJestConfig.js, you can also just do this in the module.exports = {
webpack: rewireWebpackConfig,
jest: (config) => {
config.moduleNameMapper = {
"\\.worker.js": "<rootDir>/__mocks__/workerMock.js"
};
return config;
}
}; and then in the class WorkerMock {
static default = () => {};
}
module.exports = WorkerMock; |
You could also use moduleNameMapper to map |
RE: comment from @mgiraldo #67 (comment) 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
Thanks so much @mgiraldo! I would not have figured this out without your comment. |
@bryandowning @mgiraldo Following the solutions you've given for Next JS, I'm now getting the error for mismatching API/worker versions:
Did you encounter this and how did you solve if so? EDIT: React-PDF's documentation
I found the correct CDN Version here: The lines of code that solved the issue for me:
|
in package.json made the trick for us. But here is important that we import react-pdf in the code as |
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
The text was updated successfully, but these errors were encountered: