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.

Console error "Setting up fake worker failed" while using in React

See original GitHub issue

Attach (recommended) or Link to PDF file here: https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf

Configuration:

  • Web browser and its version: Chrome 83.0.4103.116
  • Operating system and its version: MacOS 10.14.6
  • PDF.js version: 2.4.456
  • Is a browser extension: No

Steps to reproduce the problem:

  1. Using credit-react-app to create a react project https://github.com/facebook/create-react-app npm init react-app my-app
  2. Install pdfjs-dist package npm install pdfjs-dist --save
  3. Import package and copy example code to App.js (https://github.com/mozilla/pdf.js/blob/master/examples/node/getinfo.js https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples for prev/next example)
import pdfjsLib from 'pdfjs-dist/es5/build/pdf.js';
...
pdfjsLib.GlobalWorkerOptions.workerSrc = '../node_modules/pdfjs-dist/build/pdf.worker.js';
...
pdfjsLib.getDocument(url);
  1. Run react project yarn start
  2. Console shows error
Uncaught SyntaxError: Unexpected token '<' pdf.js:11915 
Uncaught (in promise) Error: Setting up fake worker failed: "Cannot read property 'WorkerMessageHandler' of undefined".
    at pdf.js:11915
Screen Shot 2020-07-06 at 9 49 47 PM

What is the expected behavior? (add screenshot) Worker is setting up correctly.

What went wrong? (add screenshot) pdfjsWorker does not exist in window object, which causes ‘WorkerMessageHandler’ of undefined issue. The switch statement started with case 0 -> case 3 -> case 6 -> case 8, ended up with catch() in _setupFakeWorker()

function setupFakeWorkerGlobal() {
...
case 8:
   return _context.abrupt("return", window.pdfjsWorker.WorkerMessageHandler);
...
}
Screen Shot 2020-07-06 at 9 50 15 PM Screen Shot 2020-07-06 at 9 50 32 PM Screen Shot 2020-07-06 at 10 01 58 PM

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension): (Trying to push code to my github, but reproducing the issue is very straight forward if following the steps. all functions are the same as example code. renderPage() etc.)

Screen Shot 2020-07-06 at 10 07 39 PM Screen Shot 2020-07-06 at 10 10 11 PM

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

4reactions
yummyelincommented, Jul 15, 2020

Hi, I don’t know why, but looks like copy pdf.worker.js to my project’s output folder, or use cdn worker will make the magic happen. Using the one from node_modules/ doesn’t work.

 // pdfjsLib.GlobalWorkerOptions.workerSrc = '../node_modules/pdfjs-dist/build/pdf.worker.js';
  pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf.worker.js`;

https://github.com/wojtekmaj/react-pdf#create-react-app

0reactions
Snuffleupaguscommented, Feb 20, 2021

Given that we know nothing about React here, is it meaningful to keep this issue open when we unfortunately cannot help? /cc @timvandermeij

Read more comments on GitHub >

github_iconTop Results From Across the Web

React pdf js - Warning: Setting up fake worker
I added the js file in the head for the worker.js i.e ... error and it starts loading the pdf Console error after...
Read more >
Setting Up Fake Worker Failed Error On Update
This error comes up if there is a caching and/or minifying plugin on the site that has cached the previous version of the...
Read more >
Setting up fake worker failed…
I get the following message when I try to load the page with my embedded pdf. Setting up fake worker failed: “Cannot load...
Read more >
Error Boundaries
A class component becomes an error boundary if it defines either (or both) of the lifecycle methods static getDerivedStateFromError() or componentDidCatch() .
Read more >
Redux Essentials, Part 5: Async Logic and Data Fetching
How to use the Redux "thunk" middleware for async logic ... the initial project setup already includes a fake in-memory REST API for...
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