Skip to content
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' from 'webpack.js' #11925

Closed
mcoates1 opened this issue May 22, 2020 · 7 comments
Closed
Labels

Comments

@mcoates1
Copy link

I am running "react": "^16.13.1" which was created using create-react-app. I am trying to run Jest tests and I keep getting the following error:

Cannot find module 'worker-loader!./build/pdf.worker.js' from 'webpack.js'

My import statement in my component looks like this:

import { PDFJS } from 'pdfjs-dist/webpack'

However, when I remove webpack from the import, the error goes away and things "seem" to work just fine.

import { PDFJS } from 'pdfjs-dist'

Can someone explain to me the difference between having webpack in the import statement vs not having it? Do I even need it? I've been trying to research this issue for days and have hit a wall so any input would be very helpful.

FYI, this is the version of pdfjs-dist that is in my package.json

"pdfjs-dist": "^2.3.200"

@scottweitzner
Copy link

@mcoates1 +1
I'm noticing this issue as well. We notice this error as a result of installing the jupyter/latex package in jupyter lab which has pdfjs-dist as an upstream dependency.

Downstream issue is discussed here

@mcoates1
Copy link
Author

mcoates1 commented Jun 4, 2020

@mcoates1 +1
I'm noticing this issue as well. We notice this error as a result of installing the jupyter/latex package in jupyter lab which has pdfjs-dist as an upstream dependency.

Downstream issue is discussed here

@scottweitzner thanks for pointing me to the downstream issue. All I know is that pdfjs-dist uses webworkers and webworkers don't work well with Jest becuase Jest uses node from what I've read. I was hoping for a solution from the team. I've tried mocks and everything else I can think of.

@scottweitzner
Copy link

@mcoates1 No problem. It looks like the issue from my end was discussed and a fix was merged here. Looks like that change will resolve the downstream issue my team was having.

I'd try pulling in the latest version once a patch release is made (most likely going to be v2.5.208). If that doesn't work then I'm afraid we might've had different issues with similar errors. Best of luck!

antoinelibert pushed a commit to exotic-matter-sas/paper-matter-app that referenced this issue Sep 4, 2020
@abhimanusharma
Copy link

I am facing same issue, any resolution yet?

@mcoates1
Copy link
Author

mcoates1 commented Oct 6, 2020

Hi @abhimanusharma . I found a workaround for this issue by keeping both

import { PDFJS } from 'pdfjs-dist/webpack'

AND

import { PDFJS } from 'pdfjs-dist'

The top works in the normal execution of the code while the bottom works with Jest. Seems like jest doesn't work well with web workers. The solution was to create an environment variable called "TESTING" or something like that and toggle between the two imports if the env variable exists or not.

Hope this helps!

@benochr
Copy link

benochr commented Jun 22, 2021

For me, adding following to the Jest configuration also worked.

moduleNameMapper: {
    'pdfjs-dist/webpack': 'pdfjs-dist',
}

@Snuffleupagus
Copy link
Collaborator

The worker-loader dependency was removed in PR #15430, so hopefully this is no longer a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants