This issue seems to arise due to esModule option introduced in [email protected].

The fix for this was merged in (pre-release) [email protected]

You can fix this by either upgrading pdfjs-dist to v2.6.347 OR downgrading worker-loader to v2.0.0

Answer from Siddhesh on Stack Overflow
🌐
Yarn
classic.yarnpkg.com › en › package › pdfjs-dist
pdfjs-dist
Important: This documentation covers Yarn 1 (Classic).
🌐
Yarn
yarnpkg.com › package
pdfjs-dist
Yarn is a package manager that doubles down as project manager. Whether you work on simple projects or industry monorepos, whether you're an open source developer or an enterprise user, Yarn has your back · First package manager built specifically ...
🌐
Yarn
classic.yarnpkg.com › en › package › pdfjs-1.4.0
Yarn
Important: This documentation covers Yarn 1 (Classic).
🌐
GitHub
github.com › mozilla › pdf.js › issues › 15302
Correct way to use pdf.js with NPM/Yarn and Webpack 5 · Issue #15302 · mozilla/pdf.js
June 4, 2022 - GlobalWorkerOptions.workerPort = new Worker(new URL('pdfjs-dist/build/pdf.worker', import.meta.url));
Published   Aug 11, 2022
Top answer
1 of 6
14

This issue seems to arise due to esModule option introduced in [email protected].

The fix for this was merged in (pre-release) [email protected]

You can fix this by either upgrading pdfjs-dist to v2.6.347 OR downgrading worker-loader to v2.0.0

2 of 6
7

I just had to solve this issue myself...

This issue

Module not found: Error: Can't resolve 'module' in '/home/giampaolo/dev/KJ_import/KJ-JS/node_modules/webpack/lib/node'

Is caused by worker-loader loading NodeTargetPlugin, which in turn runs require("module") which I think (but I'm not 100%) is for native node modules, which when running Webpack targeted for web is not relevant

This issue can be mitigated with Webpack config

{
  node: {
    module: "empty"
  }
}

Afterwards, things move along farther, but I needed further mitigations:

import pdfjsLib from "pdfjs-dist/webpack";

This runs pdfjs-dist/webpack.js:27 which is

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

Which is attempting to load pdf.worker.js (which worker-loader should be packaging) and then tries to instantiate the class:

pdfjs.GlobalWorkerOptions.workerPort = new PdfjsWorker();

The issue I had was that Webpack packaged pdf.worker.js as an esModule (the default for worker-loader), so the way it was require'd needs to be unwrapped with the default property on the imported esModule (said another way, the instantiation would have to be new PdfjsWorker.default()

I was able to mitigate this with the NormalModuleReplacementPlugin plugin, which is able to re-write the require statement based on a regex match/replace, which is matching the original require string and replacing it with one that sets the worker-loader option esModule=false, followed by the absolute path to the pdf.worker.js file on the local system:

new webpack.NormalModuleReplacementPlugin(
  /worker-loader!\.\/build\/pdf\.worker\.js$/,
  "worker-loader?esModule=false!" + path.join(__dirname, "../", "node_modules", "pdfjs-dist", "build", "pdf.worker.js")
)

It's important to match the complete original require string of /worker-loader!\.\/build\/pdf\.worker\.js$/ and not just the pdf.worker.js part, because you could end up in an infinite replace loop.

You need to fix the replacement string to be a proper path for your project, which would probably be

"worker-loader?esModule=false!" + path.join(__dirname, "node_modules", "pdfjs-dist", "build", "pdf.worker.js")

I have a ../ in my path because this code is being executed inside storybooks .storybook/ folder, so I have go up a directory to get to node_modules/

And with those two changes, everything for PDF.js seems to be working.

And lastly, if you want to ignore the warnings about the missing FetchCompileWasmPlugin and FetchCompileAsyncWasmPlugin modules, you can setup the webpack IgnorePlugin to just ignore these imports, my assumption is they're WASM based and not actually needed

plugins: [
  new webpack.IgnorePlugin({ resourceRegExp: /FetchCompileWasmPlugin$/ }),
  new webpack.IgnorePlugin({ resourceRegExp: /FetchCompileAsyncWasmPlugin$/ })
]

I'm guessing there might be some out-of-date mismatch of worker-loader and the modules in the currently installed Webpack version, but these WASM modules don't seem to be necessary for our purposes

🌐
Stack Overflow
stackoverflow.com › questions › 73619530 › does-pdfjs-dist-can-run-with-typescript
vue.js - Does pdfjs-dist can run with Typescript? - Stack Overflow
I want to preview pdf as canvas but I can't import pdfjs-dist in my project. I've already installed pdfjs-dist using $yarn add pdfjs-dist Do I have to import anything else? import pdfjsLib from "
Find elsewhere
🌐
GitHub
github.com › wojtekmaj › react-pdf › discussions › 1514
update readme with public-hoist-pattern for yarn as package manager · wojtekmaj/react-pdf · Discussion #1514
Note pnpm requires an .npmrc file with public-hoist-pattern[]=pdfjs-dist for this to work. Will this work if I am using yarn as package manager? If no, then also add config needs to be done if usin...
Author   wojtekmaj
🌐
npm
npmjs.com › package › pdfjs-dist
pdfjs-dist - npm
Generic build of Mozilla's PDF.js library.. Latest version: 5.4.449, last published: 12 days ago. Start using pdfjs-dist in your project by running `npm i pdfjs-dist`. There are 2279 other projects in the npm registry using pdfjs-dist.
      » npm install pdfjs-dist
    
Published   Nov 29, 2025
Version   5.4.449
🌐
npm
npmjs.com › package › @types › pdfjs-dist
@types/pdfjs-dist - npm
Stub TypeScript definitions entry for pdfjs-dist, which provides its own types definitions. Latest version: 2.10.378, last published: 4 years ago. Start using @types/pdfjs-dist in your project by running `npm i @types/pdfjs-dist`. There are 58 other projects in the npm registry using ...
      » npm install @types/pdfjs-dist
    
Published   Oct 28, 2021
Version   2.10.378
🌐
GitHub
github.com › vercel › next.js › issues › 58313
Import pdfjs-dist not working correctly · Issue #58313 · vercel/next.js
November 10, 2023 - The ESM package pdfjs-dist should be imported correctly. The actual outcome, however, is nothing will be imported -- all exported objects are undefined, including the default export. I verified that the issue exists in the latest Next.js canary release · Operating System: Platform: win32 Arch: x64 Version: Windows 11 Pro Binaries: Node: 21.1.0 npm: N/A Yarn: N/A pnpm: N/A Relevant Packages: next: 14.0.3-canary.1 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.3 Next.js Config: output: N/A
Published   Nov 10, 2023
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist
pdfjs-dist CDN by jsDelivr - A CDN for npm and GitHub
September 22, 2014 - A free, fast, and reliable CDN for pdfjs-dist. Generic build of Mozilla's PDF.js library.
Published   Sep 22, 2014