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
🌐
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
🌐
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
🌐
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 › 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
🌐
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
🌐
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;
🌐
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.
🌐
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 › 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 - My project has to work without outside download requests, so we had to include our own pdf.worker.min.js.
Published   Aug 07, 2019
🌐
GitHub
github.com › mozilla › pdf.js › wiki › frequently-asked-questions
Frequently Asked Questions · mozilla/pdf.js Wiki · GitHub
Including the pdf.js file locally in your project, but loading the pdf.worker.js file from a CDN.
Author   mozilla