{"id":20675,"library":"vite-plugin-node-polyfills","title":"vite-plugin-node-polyfills","description":"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.","status":"active","version":"0.26.0","language":"javascript","source_language":"en","source_url":"https://github.com/davidmyersdev/vite-plugin-node-polyfills","tags":["javascript","node","node-core-modules","node-polyfills","node-stdlib","polyfills","vite","vite-plugin","typescript"],"install":[{"cmd":"npm install vite-plugin-node-polyfills","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-node-polyfills","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-node-polyfills","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peerdep: works with Vite 2–8","package":"vite","optional":false},{"reason":"underlying polyfill library used for module shims","package":"node-stdlib-browser","optional":false}],"imports":[{"note":"Named export, not default. Also available as CommonJS via require, but ESM is preferred in Vite config.","wrong":"import nodePolyfills from 'vite-plugin-node-polyfills'","symbol":"nodePolyfills","correct":"import { nodePolyfills } from 'vite-plugin-node-polyfills'"},{"note":"Must use defineConfig wrapper for proper type inference and composition.","wrong":"export default { plugins: [nodePolyfills()] }","symbol":"vite.config.ts usage","correct":"// vite.config.ts\nimport { defineConfig } from 'vite'\nimport { nodePolyfills } from 'vite-plugin-node-polyfills'\nexport default defineConfig({ plugins: [nodePolyfills()] })"},{"note":"Options are passed as a single object argument. Separate arguments are incorrect.","wrong":"nodePolyfills({ include: ['path'] }, { globals: { Buffer: true } })","symbol":"nodePolyfills options","correct":"nodePolyfills({ include: ['path'], globals: { Buffer: true, global: true, process: true }, protocolImports: true })"},{"note":"Type is exported for use in TypeScript configs.","wrong":"","symbol":"type imports","correct":"import type { NodePolyfillsOptions } from 'vite-plugin-node-polyfills'"}],"quickstart":{"code":"// vite.config.ts\nimport { defineConfig } from 'vite'\nimport { nodePolyfills } from 'vite-plugin-node-polyfills'\n\nexport default defineConfig({\n  plugins: [\n    nodePolyfills({\n      // Enable polyfills for specific modules\n      include: ['path', 'buffer'],\n      // Polyfill globals\n      globals: {\n        Buffer: true,\n        global: true,\n        process: true,\n      },\n      // Support node: protocol imports\n      protocolImports: true,\n    }),\n  ],\n})\n\n// Then in your code (e.g., main.ts):\nimport { resolve } from 'path'\nimport { Buffer } from 'buffer'\nconsole.log(resolve('/foo', 'bar'))\nconsole.log(Buffer.from('hello').toString('hex'))","lang":"typescript","description":"Shows basic plugin setup in vite.config.ts and usage of polyfilled modules (path, buffer) in application code."},"warnings":[{"fix":"Update to v0.18.0 or later.","message":"Since v0.18.0, buffer and process shims are internalized, preventing version conflicts with Yarn v1.","severity":"breaking","affected_versions":"<0.18.0"},{"fix":"Set `globals.global: false` and use other methods to provide global if needed.","message":"The `global` polyfill may be deprecated in future; prefer explicit polyfilling.","severity":"deprecated","affected_versions":">=0.26.0"},{"fix":"Set `globals: { Buffer: true, process: true, global: true }` to enable in all modes.","message":"Globals are only polyfilled in development mode by default. For production, `globals.Buffer`, `globals.process` etc. must be explicitly set to `true` or `build`.","severity":"gotcha","affected_versions":">=0.22.0"},{"fix":"Either do not set `include` (polyfills all) or ensure all needed modules are listed.","message":"If you use `include`, modules not listed will NOT be polyfilled, possibly causing runtime errors.","severity":"gotcha","affected_versions":">=0.26.0"},{"fix":"Set `overrides: { fs: 'memfs' }` or replace 'fs' usage with browser-compatible APIs.","message":"The plugin does not polyfill `fs` in the browser; you must provide a custom override (e.g., 'memfs') or avoid using 'fs' in client code.","severity":"gotcha","affected_versions":">=0.26.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add vite-plugin-node-polyfills to vite.config.ts: `nodePolyfills({ include: ['stream'] })`","cause":"Vite externalizes Node.js core modules by default, but no polyfill is configured.","error":"Error: Module \"stream\" has been externalized for browser compatibility. Cannot access \"stream.Readable\" in client code."},{"fix":"Use `import { nodePolyfills } from 'vite-plugin-node-polyfills'`","cause":"Using default import instead of named import for `nodePolyfills`.","error":"The requested module 'vite-plugin-node-polyfills' does not provide an export named 'default'"},{"fix":"Install vite-plugin-node-polyfills and include 'buffer' in config. For types, install `@types/node` or add `\"types\": [\"node\"]` to tsconfig.","cause":"Missing polyfill for 'buffer' module, or TypeScript cannot resolve types for polyfilled module.","error":"Cannot find module 'buffer' or its corresponding type declarations."},{"fix":"In plugin config, set `globals: { process: true }`","cause":"`process` global is not polyfilled because `globals.process` is not enabled.","error":"Uncaught ReferenceError: process is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}