Overriding Dependency
@react-pdf-kit/viewer@^2.0.0 requires pdfjs-dist as a peer dependency with a default version of 5.4.530. This means you do not need to manually install pdfjs-dist unless you want to use a different version.
This guide walks you through overriding the default pdfjs-dist version in two steps:
- Install the version you need.
- Configure your specific framework to use it.
Step 1: Install pdfjs-dist
Section titled “Step 1: Install pdfjs-dist”Here is how you can install package pdfjs-dist with the latest version which is above 5.4.530.
Before configuring any framework, you must first install pdfjs-dist with the version you want to use.
This step is required regardless of which framework you use.
-
Install
pdfjs-distwith the version you want to use:Terminal window bun add pdfjs-distTerminal window npm install pdfjs-dist --saveTerminal window pnpm add pdfjs-distTerminal window yarn add pdfjs-dist -
Ensure the installed version is used by adding an override in your
package.json:package.json {"overrides": {"pdfjs-dist": "the installed version"}}package.json {"overrides": {"pdfjs-dist": "the installed version"}}package.json {"pnpm": {"overrides": {"pdfjs-dist": "the installed version"}}}package.json {"resolutions": {"pdfjs-dist": "the installed version"}}
Step 2: Configure Your Framework
Section titled “Step 2: Configure Your Framework”After installing pdfjs-dist, pick the section below that matches your project’s framework and follow the additional configuration steps.
Using React with Webpack
Section titled “Using React with Webpack”-
It’s important to complete Step 1 of
pdfjs-distinstallation before proceeding -
Install package
copy-webpack-plugin:Terminal window bun add copy-webpack-plugin --devTerminal window npm install copy-webpack-plugin --save-devTerminal window pnpm install copy-webpack-plugin -DTerminal window yarn add copy-webpack-plugin -D -
Add configurations to
webpack.config.js:webpack.config.js const CopyPlugin = require('copy-webpack-plugin')module.exports = {// ...configplugins: [new CopyPlugin({patterns: [{from: 'node_modules/pdfjs-dist/legacy/build/pdf.worker.min.mjs',to: 'pdf.worker.min.mjs'}]})/// ...other plugins]} -
Add
/pdf.worker.min.mjsto theworkerUrlprop in theRPConfigcomponent:src/App.jsx import { RPConfig } from '@react-pdf-kit/viewer'import React from 'react'function App() {return (<div><RPConfig workerUrl={'/pdf.worker.min.mjs'}>...</RPConfig></div>)}export default Appsrc/App.tsx import { RPConfig } from '@react-pdf-kit/viewer'import React from 'react'function App() {return (<div><RPConfig workerUrl={'/pdf.worker.min.mjs'}>...</RPConfig></div>)}export default App
Using React with Vite
Section titled “Using React with Vite”-
It’s important to complete Step 1 of
pdfjs-distinstallation before proceeding -
Add configurations to
vite.config.jsorvite.config.ts:vite.config.ts import { defineConfig } from 'vite'export default defineConfig({// ...configoptimizeDeps: {include: ['pdfjs-dist']// ...}}) -
Add the worker URL to the
workerUrlprop in theRPConfigcomponent:src/App.jsx import { RPConfig } from '@react-pdf-kit/viewer'import workerSrc from 'pdfjs-dist/build/pdf.worker.min.mjs?url'function AppPdfViewer() {return (<RPConfig workerUrl={workerSrc}>...</RPConfig>)}export default AppPdfViewersrc/App.tsx import { RPConfig } from '@react-pdf-kit/viewer'import workerSrc from 'pdfjs-dist/build/pdf.worker.min.mjs?url'function AppPdfViewer() {return (<RPConfig workerUrl={workerSrc}>...</RPConfig>)}export default AppPdfViewer
Next.js (v15 and later) — Turbopack
Section titled “Next.js (v15 and later) — Turbopack”-
It’s important to complete Step 1 of
pdfjs-distinstallation before proceeding -
Add the worker URL to the
workerUrlprop in theRPConfigcomponent:app/components/AppProviders.jsx import { RPConfig } from '@react-pdf-kit/viewer'import React from 'react'const workerSrc = new URL("pdfjs-dist/build/pdf.worker.min.mjs",import.meta.url,).toString();function AppProviders({ children, ...props }) {return (<RPConfig workerUrl={workerSrc} {...props}>{children}</RPConfig>)}export default AppProvidersapp/components/AppProviders.tsx import { RPConfig, RPConfigProps } from '@react-pdf-kit/viewer'import React from 'react'import { type PropsWithChildren } from 'react'const workerSrc = new URL("pdfjs-dist/build/pdf.worker.min.mjs",import.meta.url,).toString();function AppProviders({ children, ...props }: PropsWithChildren<Omit<RPConfigProps, 'workerUrl'>>) {return (<RPConfig workerUrl={workerSrc} {...props}>{children}</RPConfig>)}export default AppProviders
Next.js (v14) — Webpack
Section titled “Next.js (v14) — Webpack”-
It’s important to complete Step 1 of
pdfjs-distinstallation before proceeding -
Add configurations to
next.config.jsornext.config.ts:next.config.js const path = require('path')/** @type {import('next').NextConfig} */const nextConfig = {// Config options herewebpack: (config, { webpack }) => {config.plugins.push(new webpack.NormalModuleReplacementPlugin(/^pdfjs-dist$/, (resource) => {resource.request = path.join(__dirname, './node_modules/pdfjs-dist/webpack')}))return config}}module.exports = nextConfig;next.config.ts import { NextConfig } from "next";import path from 'path';const nextConfig: NextConfig = {/* config options here */webpack: (config, { webpack }) => {config.plugins.push(new webpack.NormalModuleReplacementPlugin(/^pdfjs-dist$/, (resource: any) => {resource.request = path.join(__dirname, './node_modules/pdfjs-dist/webpack')}))return config}};export default nextConfig; -
Add
/pdf.worker.min.mjsto theworkerUrlprop in theRPConfigcomponent:app/components/AppProviders.jsx import { RPConfig } from '@react-pdf-kit/viewer'import React from 'react'function AppProviders({ children, ...props }) {return (<RPConfig workerUrl='/pdf.worker.min.mjs' {...props}>{children}</RPConfig>)}export default AppProvidersapp/components/AppProviders.tsx import { RPConfig, RPConfigProps } from '@react-pdf-kit/viewer'import React from 'react'import { type PropsWithChildren } from 'react'function AppProviders({ children, ...props }: PropsWithChildren<Omit<RPConfigProps, 'workerUrl'>>) {return (<RPConfig workerUrl='/pdf.worker.min.mjs' {...props}>{children}</RPConfig>)}export default AppProviders
TypeScript (Vite Only)
Section titled “TypeScript (Vite Only)”This section applies only if you are using Vite with TypeScript. The ?url import suffix used in the Vite configuration above may cause a TypeScript error.
For TypeScript, you may get an error Cannot find module 'pdfjs-dist/build/pdf.worker.min.mjs?url' or its corresponding type declarations. from import import workerSrc from 'pdfjs-dist/build/pdf.worker.min.mjs?url'.
To resolve this issue, you can add file .d.ts (e.g., global.d.ts) with the content below to your project
declare module '*?url' { const content: string export default content}