react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Production Build Error: Cannot set properties of undefined (setting 'SHA224') with @react-pdf/renderer

Open VedantaKaushik opened this issue 1 year ago • 22 comments

Image

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):

  1. 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>
  1. Run the app in development mode:
npm run dev

✅ Works fine.

  1. 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

VedantaKaushik avatar Mar 06 '25 19:03 VedantaKaushik

Ditto - we're also experiencing this and have been scouring the web to find a solution. Replying here to hopefully stay updated.

bitojoe avatar Mar 06 '25 20:03 bitojoe

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;
    },

collinversluis avatar Mar 06 '25 20:03 collinversluis

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 avatar Mar 06 '25 22:03 vharadkou

After 2.5 days, I found this. Not ideal but it works.

webpack: (config) => {
        if (config.optimization) {
            config.optimization.minimize = false;
        }

        return config;
}

augur-absorb avatar Mar 06 '25 22:03 augur-absorb

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 avatar Mar 07 '25 01:03 augur-absorb

@augur-absorb Your solution worked perfectly! Thanks a lot! 🎉

VedantaKaushik avatar Mar 07 '25 05:03 VedantaKaushik

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

wroughtec avatar Mar 07 '25 14:03 wroughtec

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?

ijohnpaul2000 avatar Mar 07 '25 15:03 ijohnpaul2000

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

augur-absorb avatar Mar 07 '25 17:03 augur-absorb

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.

augur-absorb avatar Mar 07 '25 17:03 augur-absorb

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 avatar Mar 07 '25 17:03 vharadkou

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?

augur-absorb avatar Mar 07 '25 19:03 augur-absorb

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

brian-lou avatar Mar 08 '25 03:03 brian-lou

The issue seems to be fixed in the latest canary release 15.2.2-canary.4

dferber90 avatar Mar 08 '25 07:03 dferber90

issue fixed yesterday by 15.2.2 offical release ✌

AlexisAnzieu avatar Mar 13 '25 02:03 AlexisAnzieu

add this into next.config

serverExternalPackages: ['@react-pdf/renderer'],

mehdi-mirzaei avatar Mar 13 '25 09:03 mehdi-mirzaei

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

robertseghedi avatar Mar 14 '25 18:03 robertseghedi

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 avatar Mar 26 '25 15:03 victorcesae

@victorcesae Have you figured it out..?

shreeram312 avatar Apr 09 '25 08:04 shreeram312

@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

victorcesae avatar Apr 18 '25 17:04 victorcesae