-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Attach (recommended) or Link to PDF file
The error happens before any PDF is loaded, during initialization of pdfjs-dist/build/pdf.mjs.
The attached minimal PDF is only provided because the form requires it.
Attached file: empty.pdf
Web browser and its version
Chrome 121.0.6167.160 (Linux, Fedora 41)
Operating system and its version
Linux Fedora 41 (x86_64)
PDF.js version
pdfjs-dist 5.4.449
Is the bug present in the latest PDF.js version?
Yes
Is a browser extension
No
Steps to reproduce the problem
-
Create a new Next.js 16 project with App Router.
-
Install pdf.js:
pnpm add pdfjs-dist
-
Create a simple browser-only hook that dynamically imports pdfjs-dist:
// usePdfTest.ts
"use client";
import * as React from "react";export function usePdfTest() {
React.useEffect(() => {
async function load() {
try {
const pdfjs = await import("pdfjs-dist");
(pdfjs as any).GlobalWorkerOptions.workerSrc = "/pdf.worker.min.mjs";
} catch (err) {
console.error("[PdfTest] ERROR:", err);
}
}
load();
}, []);
} -
Import this hook in a client component:
// page.tsx
"use client";
import { usePdfTest } from "./usePdfTest";
export default function Page() {
usePdfTest();
returnPDF test page;
} -
Start the Next.js dev server and open the page in Chrome.
What is the expected behavior?
Importing pdfjs-dist in a browser-only environment should load the ESM bundle normally,
without throwing runtime errors before calling any API (such as getDocument).
What went wrong?
Immediately after loading the page, the following runtime error occurs:
TypeError: Object.defineProperty called on non-object
at Object.defineProperty ()
at webpack_require.r (webpack.js:1:1)
at eval (pdf.mjs:1:21)
at /node_modules/pdfjs-dist/build/pdf.mjs:28:1
The error is triggered before any pdf.js method is invoked,
even before calling getDocument.
This appears to originate from the generated bundle
pdfjs-dist/build/pdf.mjs interacting incorrectly with webpack’s
webpack_require.r ES module compatibility wrapper.
Link to a viewer
Not applicable — the issue occurs before pdf.js displays anything.
Additional context
The same project worked fine using pdfjs-dist 5.4.394.
After upgrading to 5.4.449 the error appears even when doing nothing more than:
await import("pdfjs-dist")
The stack trace points to Object.defineProperty being applied on an invalid target
during the initialization of pdf.mjs.
This suggests a packaging or ESM interoperability regression in the published
5.4.449 bundle when used under webpack (Next.js 16 app-pages-browser environment).