Skip to content

Draft: Add vite polyfill for Buffer (runtime warning)#177

Merged
mtmacdonald merged 10 commits intomasterfrom
172-buffer
Jan 6, 2026
Merged

Draft: Add vite polyfill for Buffer (runtime warning)#177
mtmacdonald merged 10 commits intomasterfrom
172-buffer

Conversation

@mtmacdonald
Copy link
Contributor

@mtmacdonald mtmacdonald commented Jan 5, 2026

Closes #172. When loading the PDF, we see a Buffer is not defined warning:

I did a quick grep of node_modules for the codeblock DevTools shows when clicking the warning. That confirms the the @react-pdf (generator package) is triggering the warning (not the wojtekmaj/react-pdf viewer component).

Screenshot 2026-01-06 at 16 29 08 Screenshot 2026-01-06 at 16 29 28

The Buffer is not defined warning happens because some frontend code is relying on Node buffer, but Vite does not provide node polyfills. They recommend opening issue tickets in any frontend packages the rely on importing node functions. I found #2758 - according to this comment there was a prior fix in PR1891, but later changes re-introduced the dependency on buffer. I also found another ticket #3225 that has been open some months. I think the conclusion there seems to be we need to polyfill buffer in our build, until the @react-pdf resolves this.

The Vite docs don't give prescriptive examples (just says "you can add polyfills manually"). The mainstream community package seems to be vite-plugin-node-polyfills. We should probably roll with that in the meantime.

Unfortunately there are still issues using rolldownvite / Vite 8 with the vite-plugin-node-polyfills package. I have opened tickets in that repo:

  • #142 Build warningYou or a plugin you are using have set optimizeDeps.esbuildOptions but this option is now deprecated (can be ignored)
  • #143 Runtime error can't access lexical declaration '__buffer_polyfill' before initialization (can be worked around with version "vite-plugin-node-polyfills": "0.22.0" in the meantime.

I guess this will settle once Vite 8 becomes mainstream and we can update then.

Seems we are also hitting #140 in CI/CD :/ (Rollup failed to resolve import "vite-plugin-node-polyfills/shims/process". I added a workaround until that's fixed.

@mtmacdonald mtmacdonald changed the title Draft: add vite polyfill for Buffer (runtime warning) Draft: Add vite polyfill for Buffer (runtime warning) Jan 6, 2026
@mtmacdonald mtmacdonald merged commit c1e2a56 into master Jan 6, 2026
1 check passed
},
},
plugins: [
nodePolyfills({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtmacdonald In my codebase for a totally different project, we're calling this as:

      nodePolyfills({
        include: ['path'],
        globals: {
          Buffer: true,
        },
      }),

so, maybe you need to add:

        globals: {
          Buffer: true,
        },

so that you don't need to manually add the manual resolve alias?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix buffer is not defined warning

2 participants