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
🌐
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
🌐
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
Find elsewhere
🌐
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.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 !.
🌐
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.
🌐
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 β€Ί 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
🌐
Snyk
snyk.io β€Ί advisor β€Ί pdfjs-dist β€Ί functions β€Ί pdfjs-dist.globalworkeroptions
How to use the pdfjs-dist.GlobalWorkerOptions function in pdfjs-dist | Snyk
import * as React from "react"; import * as CSSModules from "react-css-modules"; import * as styles from "./index.less"; import * as pdfjsLib from "pdfjs-dist"; const pdfjsViewer = require("../../../node_modules/pdfjs-dist/web/pdf_viewer.js"); // The workerSrc property shall be specified. pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.550/pdf.worker.js"; // default scale const DEFAULT_MIN_SCALE = 0.25; const DEFAULT_MAX_SCALE = 10.0; let USE_ONLY_CSS_ZOOM = true; let TEXT_LAYER_MODE = 0; // DISABLE let MAX_IMAGE_SIZE = 1024 * 1024; let CMAP_PACKED = true; let DEFAULT_URL = "/test.pdf"; let DEFAULT_SCALE_DELTA = 1.1; let MIN_SCALE = DEFAULT_MIN_SCALE; let MAX_SCALE = DEFAULT_MAX_SCALE; let DEFAULT_SCALE_VALUE: string|number = "auto"; // in order to be responsive interface IProps { url: string|object; page?: number|string; scale?: number|string;
🌐
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`;
🌐
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.
🌐
GitHub
github.com β€Ί senny β€Ί pdfjs_viewer-rails β€Ί issues β€Ί 63
pdf.worker.js is not cached by CDN Β· Issue #63 Β· senny/pdfjs_viewer-rails
April 19, 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