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.

How to embed pdf.js into another module intended for use in webpack projects?

See original GitHub issue

I created a simple module for converting PDFs to image data URLs within React apps here:

https://github.com/marcaaron/react-pdf-to-image

If I import this into a webpack project e.g. create-react-app and then build / serve this project all goes well. But in development mode I receive the following error message in JS console.

bootstrap:1 Uncaught ReferenceError: window is not defined

And webpack dev server complains about this:

./node_modules/pdfjs-dist/build/pdf.js Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Steps to reproduce the problem:

  1. Install package as dependency
  2. Attempt to use in a create-react-app project

What is the expected behavior? There would be some sort of way to add pdf.js with a worker as a dependency for other projects in development out of the box. Is this a limitation of web workers in general? Or can changes be made to the current distribution to allow this to work in all projects?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
pk-nbcommented, Dec 14, 2018

I believe this is not an issue with nesting in the package, but instead with the requireEnsure config being turned off in create-react-app.

See https://github.com/mozilla/pdf.js/issues/10253. Unfortunately the only way to adjust this config currently is to eject from CRA. We have our own config where we turned this off. I wonder if PDFjs could support dynamic imports instead going forward as they are now a standard.

0reactions
timvandermeijcommented, Dec 21, 2019

Yes, indeed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Looking for help to make npm/pdfjs-dist work with Webpack ...
I went back to a CDN. A simple <script> in the html with the pdf.min.js link, a pdfjsLib.GlobalWorkerOptions.workerSrc = " ...
Read more >
How to transpile ES modules with webpack and Node.js
Learn how webpack interacts with and supports ES modules in this deep dive tutorial on transpilation in Node.js.
Read more >
Concepts - webpack
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 >
A mostly complete guide to webpack 5 (2020)
Working with JavaScript's modules in webpack ... Once the file becomes a module, webpack can use it as a dependency in your project....
Read more >
25 reasons to switch to Webpack(er) - rossta.net
Writing your JavaScript source code within a module system allows you to take advantage of module scope within each file, i.e., no accidental ......
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