Production Build Error: Cannot set properties of undefined (setting 'SHA224') with @react-pdf/renderer
Description
The PDFDownloadLink component from @react-pdf/renderer works fine in development mode (npm run dev), but after building for production (npm run build), it throws an error when trying to generate a PDF. The error message varies, but one common issue is:
Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'SHA224')
To Reproduce
Steps to reproduce the behavior, including code snippet (if applies):
- Use the following component inside a Next.js 15 project:
<PDFDownloadLink
document={<PrintID student={student} />}
fileName={`${student.name}_ID.pdf`}
>
<button
className="bg-cyan-600 hover:bg-cyan-700 text-white font-bold py-2 px-2 rounded-lg shadow-md hover:shadow-lg transition cursor-pointer duration-300 ease-in-out sm:h-12 sm:mr-2 sm:ml-2 mt-2 mb-2 w-40"
onClick={HandleIDPrint}
>
Print ID
</button>
</PDFDownloadLink>
- Run the app in development mode:
npm run dev
✅ Works fine.
- Build and start in production mode:
npm run build && npm start
❌ Error occurs when trying to generate the PDF.
Expected behavior
PDFDownloadLink should generate and allow downloading PDFs in production mode just like it does in development.
Environment and Packages
- OS: Windows 11
- Browser: Chrome / Edge
- React-pdf version: @react-pdf/[email protected]
- Node.js version: 22 LTS
- Next.js version: 15.2.1
- React version: 19.0.0
Ditto - we're also experiencing this and have been scouring the web to find a solution. Replying here to hopefully stay updated.
We are also having this same issue. Only in production, does not occur running on dev. This issue stops if we disable minification with webpack, but obviously not ideal. bun and npm show the same error.
webpack: (config) => {
// Disable minification
config.optimization.minimize = false;
return config;
},
By the way, it works fine for next.js 15.1.7; the issue only occurs for next.js 15.2.0 and 15.2.1. This is not the first time the update to next.js has been affected by the react-pdf library. I don't know, is it an issue with next.js or react-pdf?
After 2.5 days, I found this. Not ideal but it works.
webpack: (config) => {
if (config.optimization) {
config.optimization.minimize = false;
}
return config;
}
This is much better than not minifying:
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback = {
crypto: require.resolve('crypto-browserify'),
stream: false,
buffer: false
};
}
config.optimization.minimizer = [
new TerserPlugin({
terserOptions: {
mangle: false, // Disable function renaming
keep_fnames: true, // Preserve function names (prevents SHA256 loss)
keep_classnames: true // Preserve class names (for internal PDFKit use)
}
})
];
return config;
}
@augur-absorb Your solution worked perfectly! Thanks a lot! 🎉
Doing above added almost 200kb to some pages. we only have this on one page so went for a slightly less aggressive approach (wrapped the offending component is a Errorboundary):
if (!isServer) {
config.optimization.minimizer = [
new TerserPlugin({
terserOptions: {
keep_fnames: true, // Preserve function names (prevents SHA256 loss)
},
}),
];
}
return config;
This increased the page sizes by 30-40kb (so not as bad) for us it will obviously depend on your setup
This is much better, than not minifying:
webpack: (config, { isServer }) => { if (!isServer) { config.resolve.fallback = { crypto: require.resolve('crypto-browserify'), stream: false, buffer: false }; } config.optimization.minimizer = [ new TerserPlugin({ terserOptions: { mangle: false, // Disable function renaming keep_fnames: true, // Preserve function names (prevents SHA256 loss) keep_classnames: true // Preserve class names (for internal PDFKit use) } }) ]; return config; }
how tf do you even find this solutions?
how tf do you even find this solutions?
Took me two days + to learn about the minification issue using google and chat, and then it was pretty easy after that.
@ijohnpaul2000
By the way, it works fine for next.js 15.1.7; the issue only occurs for next.js 15.2.0 and 15.2.1. This is not the first time the update to next.js has been affected by the react-pdf library. I don't know, is it an issue with next.js or react-pdf?
@vharadkou
It is JS land. Vercel super on top of the most current everything, they are constantly updating the packages for all the right reasons. Thankfully we have github issues in order to notify other teams of the updates. Though in this case the terser route works. I will do a little more research since it adds KB's to do it.
how tf do you even find this solutions?
Took me two days + to learn about the minification issue using google and chat, and then it was pretty easy after that.
It was obvious how to get a hotfix if you knew the technology you were working on (especially if you configured at least 1 time webpack by yourself). Everybody is trying to vibe code this day (especially new devs), but experienced developers can quickly analyze and provide a workaround.
The key thing is that we are here to have the proper fix for this problem, not workarounds.
how tf do you even find this solutions?
Took me two days + to learn about the minification issue using google and chat, and then it was pretty easy after that. @ijohnpaul2000
It was obvious how to get a hotfix if you knew the technology you were working on (especially if you configured at least 1 time webpack by yourself). Everybody is trying to vibe code this day (especially new devs), but experienced developers can quickly analyze and provide a workaround.
The key thing is that we are here to have the proper fix for this problem, not workarounds.
@vharadkou
I found reactPDF failing quicky. The why was not as obvious. npm patch was not the solution here. The function name is directly called, and it was getting abstracted in the build. You would not be here if you had the answer to the problem. Do you enjoy insulting people?
By the way, it works fine for next.js 15.1.7; the issue only occurs for next.js 15.2.0 and 15.2.1. This is not the first time the update to next.js has been affected by the react-pdf library. I don't know, is it an issue with next.js or react-pdf?
I'm having the same issue for my own library. This seems to be an issue with next.js
The issue seems to be fixed in the latest canary release 15.2.2-canary.4
issue fixed yesterday by 15.2.2 offical release ✌
add this into next.config
serverExternalPackages: ['@react-pdf/renderer'],
webpack: (config, { isServer }) => { if (!isServer) { config.resolve.fallback = { crypto: require.resolve('crypto-browserify'), stream: false, buffer: false }; }
config.optimization.minimizer = [ new TerserPlugin({ terserOptions: { mangle: false, // Disable function renaming keep_fnames: true, // Preserve function names (prevents SHA256 loss) keep_classnames: true // Preserve class names (for internal PDFKit use) } }) ]; return config; }
This one is valid
This is much better than not minifying:
webpack: (config, { isServer }) => { if (!isServer) { config.resolve.fallback = { crypto: require.resolve('crypto-browserify'), stream: false, buffer: false }; } config.optimization.minimizer = [ new TerserPlugin({ terserOptions: { mangle: false, // Disable function renaming keep_fnames: true, // Preserve function names (prevents SHA256 loss) keep_classnames: true // Preserve class names (for internal PDFKit use) } }) ]; return config; }
Can you send the package json of the treserplugin and crypto-browserify packages?
@victorcesae Have you figured it out..?
@victorcesae Have you figured it out..?
For me the solution was to put this in my next.config.js:
const TerserPlugin = require("terser-webpack-plugin");
webpack: (config, { isServer, dev }) => {
if(!dev) {
if (!isServer) {
config.resolve.fallback = {
crypto: require.resolve('crypto-browserify'),
stream: false,
buffer: false
};
}
config.optimization.minimizer = [
new TerserPlugin({
terserOptions: {
mangle: false, // Disable function renaming
keep_fnames: true, // Preserve function names (prevents SHA256 loss)
keep_classnames: true // Preserve class names (for internal PDFKit use)
}
})
];
}
return config;
}
and install:
npm i crypto-browserify@^3.12.1 -D --legacy-peer-deps
npm i terser-webpack-plugin@^5.3.14 -D --legacy-peer-deps