react-pdf: Can't resolve 'pdfjs-dist/build/pdf.worker.min.mjs in Nextjs 14 app router
Before you start - checklist
- I followed instructions in documentation written for my React-PDF version
- I have checked if this bug is not already reported
- I have checked if an issue is not listed in Known issues
- If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo
Description
import { Box, CircularProgress, Pagination, Stack, Typography } from ‘@mui/material’; import React, { useState } from ‘react’; import { DocumentText } from ‘iconsax-react’; import ‘react-pdf/dist/esm/Page/AnnotationLayer.css’; import ‘react-pdf/dist/esm/Page/TextLayer.css’; import { Document, Page, pdfjs } from ‘react-pdf’; pdfjs.GlobalWorkerOptions.workerSrc = new URL(‘pdfjs-dist/build/pdf.worker.min.mjs’, import.meta.url).toString();
const options = { cMapUrl: ‘/cmaps/’, standardFontDataUrl: ‘/standard_fonts/’, };
/**
- Comp
/
const CustPreviewPdf = ({ file, disablePage }: { file?: File | string; disablePage?: boolean }) => {
/*
- States */ const [totalPages, setTotalPages] = useState<number>(0); const [pageNumber, setPageNumber] = useState<number>(1);
/**
- Methods */ const onDocumentLoadSuccess = ({ totalPages }: { totalPages: number }) => { setTotalPages(totalPages); }; return ( <> <Document file={file} onLoadSuccess={(value) => onDocumentLoadSuccess({ totalPages: value.numPages })} loading={<CircularProgress />} noData={ <Stack direction={‘column’} alignItems={‘center’}> <DocumentText size={24} /> <Typography variant='subtitle2'>No PDF Imported</Typography> </Stack> } className={‘disable-pdf-select’} options={options} <Page pageNumber={pageNumber} /> </Document> {!disablePage && ( <Box sx={{ py: 1 }}> <Pagination count={totalPages} onChange={(event: React.ChangeEvent<unknown>, page: number) => { setPageNumber(page); }} /> </Box> )}
</>
); };
export default CustPreviewPdf;
Steps to reproduce
^^^^^^
`----
Caused by: 0: failed to parse input file 1: Syntax Error
Build failed because of webpack errors when I install react pdf version 9.1.0 it can’t build.
Expected behavior
⨯ node_modules/pdfjs-dist/build/pdf.mjs (5448:0) @ eval ⨯ TypeError: Promise.withResolvers is not a function at webpack_require (/Users/raibann/Documents/ONLINEISP/PROJECTS/FRONTEND/next-document-tracking/.next/server/webpack-runtime.js:33:43) at eval (./src/components/customs/media/CustPreviewPdf.tsx:17:67) at (ssr)/./src/components/customs/media/CustPreviewPdf.tsx (/Users/raibann/Documents/ONLINEISP/PROJECTS/FRONTEND/next-document-tracking/.next/server/app/new-document/page.js:642:1) at webpack_require (/Users/raibann/Documents/ONLINEISP/PROJECTS/FRONTEND/next-document-tracking/.next/server/webpack-runtime.js:33:43) at eval (./src/app/new-document/components/qrcode/index.tsx:13:98) at (ssr)/./src/app/new-document/components/qrcode/index.tsx (/Users/raibann/Documents/ONLINEISP/PROJECTS/FRONTEND/next-document-tracking/.next/server/app/new-document/page.js:433:1) at webpack_require (/Users/raibann/Documents/ONLINEISP/PROJECTS/FRONTEND/next-document-tracking/.next/server/webpack-runtime.js:33:43) at eval (./src/app/new-document/page.tsx:27:76) at (ssr)/./src/app/new-document/page.tsx (/Users/raibann/Documents/ONLINEISP/PROJECTS/FRONTEND/next-document-tracking/.next/server/app/new-document/page.js:543:1) at webpack_require (/Users/raibann/Documents/ONLINEISP/PROJECTS/FRONTEND/next-document-tracking/.next/server/webpack-runtime.js:33:43) at JSON.parse (<anonymous>) null
Actual behavior
Additional information
Environment
- Browser (if applicable):
- React-PDF version:
- React version:
- Bundler name and version (if applicable):
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 16 (2 by maintainers)
I encountered the same error when using react-pdf in my Next.js (version 14) app during the build phase. It was working perfectly in development, but the error appeared during the production build.
@UmairJibran made a valid point, and while his solution might work for some, it didn’t solve the issue for me. I fixed by replacing the following line:
with this
I switched to using the CDN, as specified in the docs
Here is the screenshot
I’ve encountered the same error too.
Building on top of @satyadalei’s solution.
You can download the CDN and put it in the
publicdirectory and call it like this.Additionally, you might run into this error
[ERROR] Promise.withResolvers is not a function Stack trace:So, you have to add the polyfill
Make sure to declare the type in your
declaration.d.tsYour component should look something like this
I’m using NextJS 14, and the way this worked for me was to import the worker first, as I was getting errors saying:
So I imported it like this:
And now it works.
Note: I am getting a TypeScript error when importing
pdfjsWorker. I’ve already added@types/pdfjs-distbut that didn’t solve the issue.If you’re using
pdfjs-distin a TypeScript project and facing type issues, follow these steps:Install
pdfjs-distRun this command to install it:
npm i pdfjs-dist
Import
pdfjs-distIn your code, import it like this:
import * as pdfjsLib from “pdfjs-dist/webpack”;
Fix TypeScript Errors
TypeScript may not recognize
pdfjs-dist/webpack. To fix this, create a type declaration file:src/types/(create thetypesfolder if it doesn’t exist).pdfjs.d.ts.pdfjs.d.ts:declare module “pdfjs-dist/webpack” { import pdfjs from “pdfjs-dist”; export = pdfjs; }
this worked for me