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
🌐
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 › getting_started
PDF.js - Getting Started
PDF.js is hosted on several free CDNs: https://www.jsdelivr.com/package/npm/pdfjs-dist · https://cdnjs.com/libraries/pdf.js · https://unpkg.com/pdfjs-dist/ Note that we only mention the most relevant files and folders.
🌐
jsDelivr
cdn.jsdelivr.net › build
pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN
pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub
🌐
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
Content delivery at its finest. cdnjs is a free and open-source CDN service trusted by over 12.5% of all websites, serving over 200 billion requests each month, powered by Cloudflare.
🌐
UNPKG
unpkg.com › pdfjs-dist
pdfjs-dist
The CDN for everything on npm
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist-viewer-min
pdfjs-dist-viewer-min CDN by jsDelivr - A CDN for npm and GitHub
July 13, 2024 - A free, fast, and reliable CDN for pdfjs-dist-viewer-min. Generic minified build of Mozilla's PDF.js library including the viewer component.
Published   Jun 19, 2016
🌐
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
March 2, 2022 - <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>PDF.js &#39;Hello, world!&#39; example, legacy version</title> <style> #the-canvas { border: 1px solid black; direction: ltr; } </style> </head> <body> <h1>PDF.js 'Hello, world!' example, legacy version</h1> <p>This example is identical to <a href="https://mozilla.github.io/pdf.js/examples/index.html#hello-world-with-document-load-error-handling">https://mozilla.github.io/pdf.js/examples/index.html#hello-world-with-document-load-error-handling</a> except the two script URLs have been replaced with CDN-hosted legacy versions of the scripts.</p> <canvas id="the-canvas"></canvas> <script src="https://cdn.jsdelivr.net/npm/[email protected]/legacy/build/pdf.min.js"></script> <script> // If absolute URL from the remote server is provided, configure the CORS // header on that server.
Published   May 10, 2022
Find elsewhere
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist-min
pdfjs-dist-min CDN by jsDelivr - A CDN for npm and GitHub
May 17, 2023 - A free, fast, and reliable CDN for pdfjs-dist-min. Generic build of Mozilla's PDF.js library.
Published   May 17, 2023
🌐
jsDelivr
cdn.jsdelivr.net › @types/[email protected]
@types/pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN
@types/pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub
🌐
GitHub
github.com › mozilla › pdf.js › issues › 5490
make pdf.js available on a cdn · Issue #5490 · mozilla/pdf.js
November 17, 2014 - Having pdf.js available on a public cdn can simplify installation and update workflows in some cases. cdnjs will likely host pdf.js for free if was ask them to. cdnjs/cdnjs#3824
Published   Nov 17, 2014
🌐
jsDelivr
cdn.jsdelivr.net › build
pdfjs-dist-di CDN by jsDelivr - A free, fast, and reliable Open Source CDN
pdfjs-dist-di CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub
🌐
jsDelivr
cdn.jsdelivr.net › build
pdfjs-dist-2 CDN by jsDelivr - A free, fast, and reliable Open Source CDN
pdfjs-dist-2 CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub
🌐
Libraries.io
libraries.io › npm › pdfjs-dist
pdfjs-dist 5.4.449 on npm - Libraries.io - security & maintenance data for open source software
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 ·
🌐
DhiWise
dhiwise.com › post › how-to-integrate-pdfjs-dist-for-pdf-rendering
A Beginner’s Guide To Pdfjs-dist Integration
December 6, 2024 - To import PDF.js in your JavaScript project, you can use a CDN or install it via NPM. The pdfjs-dist package provides a convenient way to include the library in your project, allowing you to start rendering PDFs with minimal setup.
🌐
CDNPKG
cdnpkg.com › home › pdfjs-dist › 2.2.228
PDFJS-DIST 2.2.228 CDN links - CDNPKG
1 CDN to use with PDFJS-DIST 2.2.228. Find out the best CDN to use with pdfjs-dist 2.2.228 or use multiple CDN as fallback. Simply copy and paste one of these URL !.
🌐
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
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist-for-node
pdfjs-dist-for-node CDN by jsDelivr - A CDN for npm and GitHub
December 10, 2014 - A free, fast, and reliable CDN for pdfjs-dist-for-node. Generic build of Mozilla's PDF.js library.
Published   Oct 09, 2014
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist-legacy
pdfjs-dist-legacy CDN by jsDelivr - A CDN for npm and GitHub
December 2, 2022 - A free, fast, and reliable CDN for pdfjs-dist-legacy. a legacy implementation of pdfjs-dist
Published   Dec 02, 2022