Registry / devops / vite-plugin-node-polyfills

vite-plugin-node-polyfills

JSON →
library0.26.0jsnpmunverified

Vite plugin to polyfill Node.js core modules (e.g., 'fs', 'path', 'buffer', 'stream') for browser environments. Current version 0.26.0 supports Vite 2–8. Releases follow semver with active updates. Differentiators: supports `node:` protocol imports, customizable include/exclude/override lists, global polyfill options, and internalized shims to avoid dependency conflicts (since v0.18.0). Ships TypeScript types.

npm install vite-plugin-node-polyfills
INSTALL
IMPORT
SIG · VITE-PLUGIN-NODE-P
V
vite-plugin-node-polyfills
devopsjavascriptv0.26.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

nodePolyfills
import { nodePolyfills } from 'vite-plugin-node-polyfills'
import nodePolyfills from 'vite-plugin-node-polyfills'
Named export, not default. Also available as CommonJS via require, but ESM is preferred in Vite config.
vite.config.ts usage
// vite.config.ts import { defineConfig } from 'vite' import { nodePolyfills } from 'vite-plugin-node-polyfills' export default defineConfig({ plugins: [nodePolyfills()] })
export default { plugins: [nodePolyfills()] }
Must use defineConfig wrapper for proper type inference and composition.
nodePolyfills options
nodePolyfills({ include: ['path'], globals: { Buffer: true, global: true, process: true }, protocolImports: true })
nodePolyfills({ include: ['path'] }, { globals: { Buffer: true } })
Options are passed as a single object argument. Separate arguments are incorrect.
type imports
import type { NodePolyfillsOptions } from 'vite-plugin-node-polyfills'
Type is exported for use in TypeScript configs.

Shows basic plugin setup in vite.config.ts and usage of polyfilled modules (path, buffer) in application code.

// vite.config.ts import { defineConfig } from 'vite' import { nodePolyfills } from 'vite-plugin-node-polyfills' export default defineConfig({ plugins: [ nodePolyfills({ // Enable polyfills for specific modules include: ['path', 'buffer'], // Polyfill globals globals: { Buffer: true, global: true, process: true, }, // Support node: protocol imports protocolImports: true, }), ], }) // Then in your code (e.g., main.ts): import { resolve } from 'path' import { Buffer } from 'buffer' console.log(resolve('/foo', 'bar')) console.log(Buffer.from('hello').toString('hex'))
Debug
Known issues
breakingSince v0.18.0, buffer and process shims are internalized, preventing version conflicts with Yarn v1.
fix
Update to v0.18.0 or later.
affects: <0.18.0
deprecatedThe `global` polyfill may be deprecated in future; prefer explicit polyfilling.
fix
Set `globals.global: false` and use other methods to provide global if needed.
affects: >=0.26.0
gotchaGlobals are only polyfilled in development mode by default. For production, `globals.Buffer`, `globals.process` etc. must be explicitly set to `true` or `build`.
fix
Set `globals: { Buffer: true, process: true, global: true }` to enable in all modes.
affects: >=0.22.0
gotchaIf you use `include`, modules not listed will NOT be polyfilled, possibly causing runtime errors.
fix
Either do not set `include` (polyfills all) or ensure all needed modules are listed.
affects: >=0.26.0
gotchaThe plugin does not polyfill `fs` in the browser; you must provide a custom override (e.g., 'memfs') or avoid using 'fs' in client code.
fix
Set `overrides: { fs: 'memfs' }` or replace 'fs' usage with browser-compatible APIs.
affects: >=0.26.0
Errors
Common errors & fixes
Error: Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code.
Vite externalizes Node.js core modules by default, but no polyfill is configured.
fix
Add vite-plugin-node-polyfills to vite.config.ts: `nodePolyfills({ include: ['stream'] })`
The requested module 'vite-plugin-node-polyfills' does not provide an export named 'default'
Using default import instead of named import for `nodePolyfills`.
fix
Use `import { nodePolyfills } from 'vite-plugin-node-polyfills'`
Cannot find module 'buffer' or its corresponding type declarations.
Missing polyfill for 'buffer' module, or TypeScript cannot resolve types for polyfilled module.
fix
Install vite-plugin-node-polyfills and include 'buffer' in config. For types, install `@types/node` or add `"types": ["node"]` to tsconfig.
Uncaught ReferenceError: process is not defined
`process` global is not polyfilled because `globals.process` is not enabled.
fix
In plugin config, set `globals: { process: true }`
Upgrade
Version history
0.26.0latest on npm
Audit
Dependencies
viterequiredpeerdep: works with Vite 2–8
node-stdlib-browserrequiredunderlying polyfill library used for module shims
Agent activity
14 hits · last 30 days
node
10
OpenAI (training)
1
Resources
vite-plugin-node-polyfills — npm install vite-plugin-node-polyfills · libregistry