question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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"

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

2reactions
benochrcommented, Jun 22, 2021

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

moduleNameMapper: {
    'pdfjs-dist/webpack': 'pdfjs-dist',
}
1reaction
mcoates1commented, 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sign up
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.
Read more >
Looking for help to make npm/pdfjs-dist work with Webpack ...
Module not found: Error: Can't resolve 'module' in ... Which is attempting to load pdf.worker.js (which worker-loader should be packaging) ...
Read more >
Compile and set the worker source with Webpack
This post demonstrates a simple way that uses the Webpack bundler to compile and set up the worker source based on the current...
Read more >
Web Workers | webpack - JS.ORG
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
How to can I view pdf file on website?
Error: Cannot find module 'worker-loader!./build/pdf.worker.js' at makeMissingError (modules-runtime.js?hash= ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found