You have to set GlobalWorkerOptions.workerSrc to /build/pdf.worker(.min).js of same version:

pdfjsLib.GlobalWorkerOptions.workerSrc =
  "https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.min.js";  

pdfjsLib.getDocument('./ahmed.pdf').promise.then(doc => {
  console.log(`This document has ${doc._pdfInfo.numPages} pages.");
});

And, as @Pasi has mentioned, you have to promisify .getDocument() by chaining .promise on it. Without it, there is no .then().

Answer from tao on Stack Overflow
🌐
cdnjs
cdnjs.com › home › libraries › pdf.js › 2.6.347
pdf.js - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers
https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf_viewer.css · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf_viewer.js · https://cdnjs.cloudfla...
🌐
jsDelivr
jsdelivr.com › package › npm › pdf.worker
pdf.worker CDN by jsDelivr - A CDN for npm and GitHub
October 10, 2023 - A free, fast, and reliable CDN for pdf.worker. This is a required file for pdf js to work
Published   Oct 10, 2023
Discussions

How to import pdf.js using cdn
I wanted to use pdf.js for a project of mine but I faced an issue of importing it, basically, the CDN doesn't work Here is my code More on stackoverflow.com
🌐 stackoverflow.com
Shouldn't ng2-pdf-viewer use pdf.worker.min.js from pdfjs-dist library?
Bug Report or Feature Request (mark with an x) - [ ] Regression (a behavior that used to work and stopped working in a new release) - [x] Bug report -> please search issues before submitting - [ ] ... More on github.com
🌐 github.com
22
August 7, 2019
Compressed pdf.worker.min.js works with errors
The problem begins when I use legacy/pdf.worker.min.js file instead of pdf.worker.js (downloaded from https://www.jsdelivr.com/package/npm/pdfjs-dist). Steps to reproduce the problem: Replace pretty code of build/pdf.worker.js with code from https://cdn.jsdelivr.net/npm/[email protected]/l... More on github.com
🌐 github.com
5
October 8, 2022
Is there any CDN url for PDFJS express library
Hi Team, We are giving the web-viewer folder library path for rendering the pdf. In local it is working. But in our server we are getting path issue. In our angular project we are using only main.js. Do you have CDN url for the library so that we don’t want to store the lib folder in our ... More on pdfjs.community
🌐 pdfjs.community
0
May 30, 2021
🌐
jsDelivr
cdn.jsdelivr.net › build
dw-mx-pdfjs CDN by jsDelivr - A free, fast, and reliable Open Source CDN
Free Open Source CDN for dw-mx-pdfjs · Looking for a nice landing page for your package? https://www.jsdelivr.com/package/npm/dw-mx-pdfjs
🌐
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
🌐
Mozilla
mozilla.github.io › pdf.js
PDF.js - Home
A general-purpose, web standards-based platform for parsing and rendering PDFs.
🌐
GitHub
github.com › VadimDez › ng2-pdf-viewer › issues › 504
Shouldn't ng2-pdf-viewer use pdf.worker.min.js from pdfjs-dist library? · Issue #504 · VadimDez/ng2-pdf-viewer
August 7, 2019 - As pdfjs-dist library (which is included in ng2-pdf-viewer dependencies) already comes with its version of pdf.worker.min.js, wouldn't it be better for ng2-pdf-viewer to use it instead of downloading it from an external source?
Published   Aug 07, 2019
Find elsewhere
🌐
jsDelivr
cdn.jsdelivr.net › build
pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN
Free Open Source CDN for pdfjs-dist · Looking for a nice landing page for your package? https://www.jsdelivr.com/package/npm/pdfjs-dist
🌐
CDNPKG
cdnpkg.com › home › pdf.js › pdf.worker.min.js
PDF.WORKER.MIN.JS: DOWNLOAD - CDNPKG
Download pdf.worker.min.js or get a CDN url for 1510 versions of pdf.js.
🌐
GitHub
github.com › mozilla › pdf.js › issues › 15790
Compressed pdf.worker.min.js works with errors #15790
October 8, 2022 - The problem begins when I use legacy/pdf.worker.min.js file instead of pdf.worker.js (downloaded from https://www.jsdelivr.com/package/npm/pdfjs-dist). Steps to reproduce the problem: Replace pretty code of build/pdf.worker.js with code from https://cdn.jsdelivr.net/npm/[email protected]/l...
Published   Dec 07, 2022
🌐
npm
npmjs.com › package › react-pdf
react-pdf - npm
October 9, 2025 - For React-PDF to work, PDF.js worker needs to be provided. You have several options. ... import { pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = new URL( 'pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url, ).toString();
      » npm install react-pdf
    
Published   Oct 09, 2025
Version   10.2.0
Author   Wojciech Maj
🌐
PDF.js Express
pdfjs.community › technical support
Is there any CDN url for PDFJS express library - Technical Support - PDF.js Express
May 30, 2021 - Hi Team, We are giving the web-viewer folder library path for rendering the pdf. In local it is working. But in our server we are getting path issue. In our angular project we are using only main.js. Do you have CDN url for the library so that we don’t want to store the lib folder in our project and our path issue will be solved.
🌐
Mozilla
mozilla.github.io › pdf.js › examples
PDF.js - Examples
This tutorial shows how PDF.js can be used as a library in a web browser.
🌐
GitHub
github.com › wojtekmaj › react-pdf
GitHub - wojtekmaj/react-pdf: Display PDFs in your React app as easily as if they were images.
For React-PDF to work, PDF.js worker needs to be provided. You have several options. For most cases, the following example will work: import { pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = new URL( 'pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url, ).toString(); Warning ·
Starred by 10.7K users
Forked by 981 users
Languages   TypeScript 94.3% | CSS 5.6% | HTML 0.1%
🌐
Reddit
reddit.com › r/nextjs › how do i solve this issue: error: setting up fake worker failed: "cannot find module '/pdf.worker.js' ?
r/nextjs on Reddit: How do I solve this issue: Error: Setting up fake worker failed: "Cannot find module '/pdf.worker.js' ?
August 3, 2023 - React-pdf uses under the hood pdfjs, and you should follow their configuration instructions in order to properly make it work for Next.js: import { pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = new URL( 'pdfjs-dist/build/pdf.worker.min.js', import.meta.url, ).toString();
🌐
npm
npmjs.com › package › pdfjs-dist
pdfjs-dist - npm
2 weeks ago - Generic build of Mozilla's PDF.js library.. Latest version: 5.4.449, last published: 14 days ago. Start using pdfjs-dist in your project by running `npm i pdfjs-dist`. There are 2281 other projects in the npm registry using pdfjs-dist.
      » npm install pdfjs-dist
    
Published   Nov 29, 2025
Version   5.4.449
🌐
React PDF Viewer
react-pdf-viewer.dev › docs › basic-usage
Basic usage - React PDF Viewer
https://unpkg.com/[email protected]/build/pdf.worker.min.js · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js · or download and store it on your server. Note · It's very important to note that the worker version and the `pdfjs` package (mentioned in the Install pdfjs library section of Getting started) have to be the same.