Here's a super ugly workaround for client-components, until something better is proposed (I hate this too, but it works):

npm i pdfjs-dist

Edit 1: A hook might be better for reusability:

Create Your Hook

"use client";
import {useEffect} from "react";
import * as PDFJS from "pdfjs-dist/types/src/pdf";

export const usePDFJS = (onLoad: (pdfjs: typeof PDFJS) => Promise<void>, deps: (string | number | boolean | undefined | null)[] = []) => {
  
  const [pdfjs, setPDFJS] = useState<typeof PDFJS>(null);
  
  // load the library once on mount (the webpack import automatically sets-up the worker)
  useEffect(() => {
    import("pdfjs-dist/webpack.mjs").then(setPDFJS)
  }, []);

  // execute the callback function whenever PDFJS loads (or a custom dependency-array updates)
  useEffect(() => {
    if(!pdfjs) return;
    (async () => await onLoad(pdfjs))();
  }, [ pdfjs, ...deps ]);
}

Typescript Fix

Your compiler might complain about a typescript issue; if so, I just added an index.d.ts (note the .d.ts) at the same level as my hook:

declare module 'pdfjs-dist/webpack.mjs' { export * from 'pdfjs-dist' }

Use Your Hook

"use client";
import {usePDFJS} from "...";


export default function Page() {
  usePDFJS(async (pdfjs) => {
    console.log(pdfjs)
  })
}
Answer from Nicholas Barrow on Stack Overflow
🌐
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: 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
🌐
GitHub
github.com › mozilla › pdfjs-dist
GitHub - mozilla/pdfjs-dist: Generic build of PDF.js library.
July 14, 2024 - Generic build of PDF.js library. . Contribute to mozilla/pdfjs-dist development by creating an account on GitHub.
Starred by 1.3K users
Forked by 568 users
Languages   JavaScript 99.2% | CSS 0.8%
🌐
React PDF Viewer
react-pdf-viewer.dev › docs › getting-started
Getting started - React PDF Viewer
The latest version of `pdfjs-dist` (`3.4.120`) uses some modern JavaScript features. It requires additional configurations.
🌐
Reddit
reddit.com › r/sveltejs › errors when running pdf.js / pdfjs-dist... but only in build
r/sveltejs on Reddit: Errors when running PDF.js / pdfjs-dist... but only in build
September 6, 2023 -

I'm looking to add image thumbnails of PDFs to my project. So naturally, I grabbed PDF.js. After noodling through a few quirks (using the -dist version, resolving API/worker mismatch), I got it to work. Looked great in dev & in preview.

However, after deploying my build, `Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'pdfjs-dist' imported from ...\build\server\chunks\_page.svelte-2b3b8113.js`.

The site runs initially, and seems to work, but after any refresh, the above error is logged, and the user gets a nice 500 Internal Error message.

This is how I'm using it in my simple component:
` import * as PDFJS from 'pdfjs-dist';
PDFJS.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';`

Any easy way to resolve this error? Or another package I can use that's more Svelte (Vite?) friendly?

Top answer
1 of 6
9

Here's a super ugly workaround for client-components, until something better is proposed (I hate this too, but it works):

npm i pdfjs-dist

Edit 1: A hook might be better for reusability:

Create Your Hook

"use client";
import {useEffect} from "react";
import * as PDFJS from "pdfjs-dist/types/src/pdf";

export const usePDFJS = (onLoad: (pdfjs: typeof PDFJS) => Promise<void>, deps: (string | number | boolean | undefined | null)[] = []) => {
  
  const [pdfjs, setPDFJS] = useState<typeof PDFJS>(null);
  
  // load the library once on mount (the webpack import automatically sets-up the worker)
  useEffect(() => {
    import("pdfjs-dist/webpack.mjs").then(setPDFJS)
  }, []);

  // execute the callback function whenever PDFJS loads (or a custom dependency-array updates)
  useEffect(() => {
    if(!pdfjs) return;
    (async () => await onLoad(pdfjs))();
  }, [ pdfjs, ...deps ]);
}

Typescript Fix

Your compiler might complain about a typescript issue; if so, I just added an index.d.ts (note the .d.ts) at the same level as my hook:

declare module 'pdfjs-dist/webpack.mjs' { export * from 'pdfjs-dist' }

Use Your Hook

"use client";
import {usePDFJS} from "...";


export default function Page() {
  usePDFJS(async (pdfjs) => {
    console.log(pdfjs)
  })
}
2 of 6
4

There's a pretty lengthy discussion over on Github issues where people have managed to get it working either on client:

I decided to follow a simple path, I downloaded the stable version from the official website. I put all the files in the public folder. Then I added this tag to my component:

<script src="/pdfjs/pdf.mjs" type="module" />

then adding code in useEffect:

  const pdfjs = window.pdfjsLib as typeof import('pdfjs-dist/types/src/pdf')
  const pdfjsWorker = await import('pdfjs-dist/build/pdf.worker.min.mjs');
  pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

  const pdfDocument = pdfjs.getDocument('http://localhost:3000/pdf-files/myFile.pdf')

  console.log('pdfDocument', pdfDocument);

Or server-side, via appDir (e.g. app/api/pdf/route.js)

import * as pdfjs from 'pdfjs-dist/build/pdf.min.mjs';
await import('pdfjs-dist/build/pdf.worker.min.mjs');

export async function POST(req, res) {
  const pdf = await pdfjs.getDocument(
    'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'
  ).promise;
  const page = await pdf.getPage(1);
  const textContent = await page.getTextContent();
  return NextResponse.json({ message: textContent }, { status: 200 });
}

I've personally just tested this last API-one on Next.js 14.1.1 and it works just fine after a npm install pdfjs-dist

🌐
Libraries.io
libraries.io › npm › pdfjs-dist
pdfjs-dist 5.4.449 on npm - Libraries.io - security & maintenance data for open source software
September 22, 2014 - Generic build of Mozilla's PDF.js library. - 5.4.449 - a JavaScript package on npm
Find elsewhere
🌐
Mozilla
mozilla.github.io › pdf.js › getting_started
PDF.js - Getting Started
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.
🌐
CloudDefense.ai
clouddefense.ai › code › javascript › example › pdfjs-dist
Top 10 Examples of pdfjs-dist code in Javascript
init (timestamp) { const t = this; this.loadingTask = pdfJsLib.getDocument({ url: this.url, cMapUrl: '../../node_modules/pdfjs-dist/cmaps/', cMapPacked: true }); this.loadingTask.promise.then(async function(pdf) { console.time('PDF_Render') console.log('PDF loaded'); var container = document.querySelector(`.pdf-wrap`); // for(var i = 0, len = pdf.numPages; i &lt; len; i++) { // await t.renderPage(pdf, i + 1, container) // } console.log(pdf.numPages); var pages = await Promise.all( Array.apply(null, Array(pdf.numPages)).map((item, index) =&gt; { console.log(item, index)
🌐
Nutrient
nutrient.io › blog › sdk › how to build a reactjs viewer with pdfjs
Build a React PDF viewer with PDF.js and Next.js: Step-by-step tutorial
July 16, 2025 - Run the following command to scaffold a new Next.js project inside a folder named pdfjs-demo: ... When prompted, press Enter to accept the default options. This will set up the project with the recommended settings, allowing you to focus on integrating PDF.js with Next.js. Next, navigate into your project directory and install the distribution build of PDF.js, available on npm as pdfjs-dist(opens in a new tab):
🌐
GitHub
github.com › mozilla › pdf.js › wiki › setup-pdf.js-in-a-website
Setup PDF.js in a website
May 31, 2024 - npm install pdfjs-dist --save · You can find a usage example at https://github.com/mozilla/pdf.js/blob/master/examples/node/getinfo.mjs. Install the PDF.js dependency in your project: npm install pdfjs-dist --save-dev · To use the library in your project add require('pdfjs-dist') to your file requires and build your project normally.
Author   mozilla
🌐
UNPKG
unpkg.com › pdfjs-dist
pdfjs-dist
Generic build of Mozilla's PDF.js library · mozilla.github.io/pdf.js/
🌐
CodeSandbox
codesandbox.io › examples › package › pdfjs-dist
pdfjs-dist examples - CodeSandbox
Use this online pdfjs-dist playground to view and fork pdfjs-dist example apps and templates on CodeSandbox.
🌐
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 › wojtekmaj › react-pdf › discussions › 1520
Mismatching worker version with pdfjs-dist as a direct dependency (answered ✅) · wojtekmaj/react-pdf · Discussion #1520
In this case I have this "pdfjs-dist": "3.7.107" in my package.json, but react-pdf depends on "pdfjs-dist": "3.6.172".
Author   wojtekmaj
🌐
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.
🌐
jsDelivr
jsdelivr.com › package › npm › @bundled-es-modules › pdfjs-dist
@bundled-es-modules/pdfjs-dist CDN by jsDelivr - A CDN for npm and GitHub
May 12, 2023 - A free, fast, and reliable CDN for @bundled-es-modules/pdfjs-dist. mirror of pdfjs-dist, bundled and exposed as ES module
Published   Dec 10, 2018
🌐
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
🌐
Mozilla
mozilla.github.io › pdf.js
PDF.js - Home
A general-purpose, web standards-based platform for parsing and rendering PDFs.