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.

Support Web Workers coming from node_modules

See original GitHub issue

Choose one: is this a 🐛 bug report or 🙋 feature request?

Both, I guess?

🤔 Expected Behavior

  • node_modules should be also checked when looking for a file

😯 Current Behavior

  • trying to refer to a Service Worker file from node_modules fails with the following error:
×  C:\Users\wojtekmaj\Projekty\react-pdf\sample\parcel\node_modules\react-pdf\build\entry.parcel.js: Cannot resolve dependency './pdfjs-dist\build\pdf.worker.js' (...)

what it is trying to do is look for the path in ./ but not in node_modules.

🔦 Context

💻 Code Sample

I’m currently working on support for Parcel in React-PDF. That requires me to handle loading a service worker which is not mine.

In Webpack, the following code does the job:

var pdfjs = require('react-pdf/build/pdf.js');
var PdfjsWorker = require('worker-loader!react-pdf/build/pdf.worker.js');

if (typeof window !== 'undefined' && 'Worker' in window) {
  pdfjs.PDFJS.workerPort = new PdfjsWorker();
}

I was trying to reproduce the same using Parcel, and here’s what I got so far:

const pdfjs = require('pdfjs-dist');

if (typeof window !== 'undefined' && 'Worker' in window) {
  pdfjs.PDFJS.workerPort = new Worker('pdfjs-dist/build/pdf.worker.js');
}

This results in an error described below. Changing the line to

pdfjs.PDFJS.workerPort = new Worker('../node_modules/pdfjs-dist/build/pdf.worker.js');

stops the error, but:

  • I’m not comfortable with relative path in a package like that
  • This caused… a 404 error! Parcel starts referring to a file it did not produced.

🌍 Your Environment

Software Version(s)
Parcel 1.5.0
Node 9.3.0
npm/Yarn npm, newest
Operating System Windows 10

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
MikeYermolayevcommented, Nov 17, 2019

@mischnic yep. Had to reject using that library

2reactions
mischniccommented, Feb 25, 2018

@devongovett Inspired by the new resolver:

  • "~/node_modules/pdfjs-dist/build/pdf.worker.js" for “[…/]node_modules/pdfjs-dist/build/pdf.worker.js”
  • "lib/worker.js" = "./lib/worker.js" for relative paths
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use a web-worker from an npm library into main ...
If I use this in my main project and modify the web workers in the dist output from this: new URL("my.worker-8e6a623b.js", ({ url: ......
Read more >
Using Web Workers - Web APIs - MDN Web Docs
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering ......
Read more >
Seamless web workers & worker threads - threads.js
This code will run in any node.js version that comes with worker threads support, so node 12+ or node 10+ with a feature...
Read more >
Supporting Web Workers API in Node.js vs Just Using Deno
Node.js already provides support for this in the form of Worker Threads, but because it's a different implementation than the browser API, ...
Read more >
Multithreading in JavaScript with Web Workers - LeanyLabs
Web workers are handy for doing long computations in the background. Typically, where everything runs in one thread, those computations would block everything ......
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