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.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf_viewer.js.map ·
🌐
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
🌐
Mozilla
mozilla.github.io › pdf.js › getting_started
PDF.js - Getting Started
https://cdnjs.com/libraries/pdf.js · https://unpkg.com/pdfjs-dist/ Note that we only mention the most relevant files and folders. ├── build/ │ ├── pdf.mjs - display layer │ ├── pdf.mjs.map - display layer's source map │ ├── pdf.worker.mjs - core layer │ └── pdf.worker.mjs.map - core layer's source map ├── web/ │ ├── cmaps/ - character maps (required by core) │ ├── compressed.tracemonkey-pldi-09.pdf - PDF file for testing purposes │ ├── images/ - images for the viewer and annotation icons │ ├── locale/ - translation files │ ├── viewer.css - viewer style sheet │ ├── viewer.html - viewer layout │ ├── viewer.mjs - viewer layer │ └── viewer.mjs.map - viewer layer's source map └── LICENSE ·
🌐
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
🌐
GitHub
github.com › mozilla › pdf.js
GitHub - mozilla/pdf.js: PDF Reader in JavaScript
This will generate pdf.js and pdf.worker.js in the build/generic/build/ directory (respectively build/generic-legacy/build/). Both scripts are needed but only pdf.js needs to be included since pdf.worker.js will be loaded by pdf.js. The PDF.js files are large and should be minified for production. To use PDF.js in a web application you can choose to use a pre-built version of the library or to build it from source. We supply pre-built versions for usage with NPM under the pdfjs-dist name. For more information and examples please refer to the wiki page on this subject. PDF.js is hosted on several free CDNs: https://www.jsdelivr.com/package/npm/pdfjs-dist ·
Starred by 52.4K users
Forked by 10.5K users
Languages   JavaScript 71.3% | Fluent 25.5% | CSS 2.2%
🌐
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.
Find elsewhere
🌐
Skool
skool.com › universityofcode › solved-pdfjs-worker-optiosn-url-issue
SOLVED pdfjs worker optiosn url issue · University of Code
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;
🌐
GitHub
github.com › mikecousins › react-pdf-js › issues › 424
PDF Worker used CDN URL · Issue #424 · mikecousins/react-pdf-js
June 11, 2023 - The package is not working with CDN URL as our project is based on PWA . Can we have a package without CDN or can we set the pdfworker path or any other solution?
Published   Aug 09, 2023
🌐
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
🌐
GitHub
github.com › mozilla › pdf.js › issues › 15790
Compressed pdf.worker.min.js works with errors · Issue #15790 · mozilla/pdf.js
October 8, 2022 - Replace pretty code of build/pdf.worker.js with code from https://cdn.jsdelivr.net/npm/[email protected]/legacy/build/pdf.worker.min.js
Published   Dec 07, 2022
🌐
npm
npmjs.com › package › react-pdf › v › 5.3.0
react-pdf - npm
Alternatively, you could use the minified pdf.worker.min.js from an external CDN:
      » npm install react-pdf
    
Published   Aug 15, 2025
Version   5.3.0
Author   Wojciech Maj
🌐
Javascripting
javascripting.com › view › pdf-js
Pdf.js - JavaScripting
This will generate pdf.js and pdf.worker.js in the build/generic/build/ directory (respectively build/generic-legacy/build/). Both scripts are needed but only pdf.js needs to be included since pdf.worker.js will be loaded by pdf.js. The PDF.js files are large and should be minified for production. To use PDF.js in a web application you can choose to use a pre-built version of the library or to build it from source. We supply pre-built versions for usage with NPM and Bower under the pdfjs-dist name. For more information and examples please refer to the wiki page on this subject. PDF.js is hosted on several free CDNs: https://www.jsdelivr.com/package/npm/pdfjs-dist ·
🌐
GitHub
github.com › senny › pdfjs_viewer-rails › issues › 63
pdf.worker.js is not cached by CDN · Issue #63 · senny/pdfjs_viewer-rails
April 16, 2020 - I have configured CDN for my app, but pdf.worker.js and other static files are still hitting the rails server. How do I avoid this?
Published   May 27, 2020
🌐
CDNPKG
cdnpkg.com › home › pdf.js
PDF.JS CDN links [mozilla/pdf.js] - CDNPKG
1 CDN to use with PDF.JS (mozilla/pdf.js) . Find out the best CDN to use with pdf.js or use multiple CDN as fallback. Simply copy and paste one of these URL !.
🌐
GitHub
github.com › mozilla › pdf.js › issues › 14900
CDN-hosted legacy version of pdfjs-dist has syntax errors in IE · Issue #14900 · mozilla/pdf.js
February 27, 2022 - pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/[email protected]/legacy/build/pdf.worker.min.js'; // Asynchronous download of PDF var loadingTask = pdfjsLib.getDocument(url); loadingTask.promise.then(function(pdf) { console.log('PDF loaded'); // Fetch the first page var pageNumber = 1; pdf.getPage(pageNumber).then(function(page) { console.log('Page loaded'); var scale = 1.5; var viewport = page.getViewport({scale: scale}); // Prepare canvas using PDF page dimensions var canvas = document.getElementById('the-canvas'); var context = canvas.getContext('2d'); canvas.heigh
Published   May 10, 2022
🌐
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.