Draft: Add vite polyfill for Buffer (runtime warning)#177
Merged
mtmacdonald merged 10 commits intomasterfrom Jan 6, 2026
Merged
Draft: Add vite polyfill for Buffer (runtime warning)#177mtmacdonald merged 10 commits intomasterfrom
mtmacdonald merged 10 commits intomasterfrom
Conversation
| }, | ||
| }, | ||
| plugins: [ | ||
| nodePolyfills({ |
There was a problem hiding this comment.
@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?
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #172. When loading the PDF, we see a
Buffer is not definedwarning:I did a quick grep of
node_modulesfor 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).The
Buffer is not definedwarning 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 onbuffer. I also found another ticket #3225 that has been open some months. I think the conclusion there seems to be we need to polyfillbufferin our build, until the@react-pdfresolves 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-polyfillspackage. I have opened tickets in that repo:You or a plugin you are using have set optimizeDeps.esbuildOptions but this option is now deprecated(can be ignored)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.