Vite Plugin for Node.js Standard Library Browser Polyfills
vite-plugin-node-stdlib-browser, currently at version 0.2.1, is a Vite plugin designed to integrate `node-stdlib-browser` into Vite projects. Its primary function is to provide browser polyfills for Node.js built-in modules, enabling web applications to run code that implicitly or explicitly relies on these Node.js APIs within a browser environment. The plugin maintains a relatively slow release cadence, typical for a utility plugin that wraps another library, and its current version is still in an early development stage, as indicated by the author's disclaimer regarding limited testing in complex projects. A key differentiator is its explicit design choice to *not* support `node:` protocol imports and to *only* polyfill dependencies, not direct user code. This intentional limitation sets it apart from alternatives like `vite-plugin-node-polyfills`, which does offer `node:` protocol support. Developers should be aware of these architectural decisions when choosing a polyfill solution.
Common errors
-
ReferenceError: Buffer is not defined
cause A browser environment attempted to execute code that relies on Node.js global objects or built-in modules (e.g., `Buffer`, `process`, `global`) without proper polyfilling.fixEnsure `vite-plugin-node-stdlib-browser` is correctly installed as a dev dependency and `node-stdlib-browser` as a regular dependency. Verify the plugin is correctly configured and added to the `plugins` array in your `vite.config.js`. -
Failed to resolve import "node:fs" from "..."
cause The application code or a dependency is attempting to use a `node:` protocol import (e.g., `node:fs`, `node:path`), which is explicitly not supported by `vite-plugin-node-stdlib-browser`.fixRefactor the code to avoid `node:` protocol imports. If `node:` protocol support is essential for your project, consider using an alternative plugin like `vite-plugin-node-polyfills` which offers this functionality.
Warnings
- gotcha This plugin explicitly does NOT support `node:` protocol imports (e.g., `import fs from 'node:fs'`). If your code or dependencies use this syntax, they will not be polyfilled.
- gotcha The plugin is intentionally designed to polyfill Node.js built-in libraries only within your project's *dependencies*, not directly within your user-written application code.
- gotcha The plugin's author notes that it has only been tested in 'simple projects.' This implies potential unforeseen issues or instability when integrated into more complex or edge-case Vite configurations.
Install
-
npm install vite-plugin-node-stdlib-browser -
yarn add vite-plugin-node-stdlib-browser -
pnpm add vite-plugin-node-stdlib-browser
Imports
- nodePolyfills
const nodePolyfills = require('vite-plugin-node-stdlib-browser')import nodePolyfills from 'vite-plugin-node-stdlib-browser'
- defineConfig
const { defineConfig } = require('vite')import { defineConfig } from 'vite'
Quickstart
import { defineConfig } from 'vite'
import nodePolyfills from 'vite-plugin-node-stdlib-browser'
export default defineConfig({
plugins: [
nodePolyfills()
]
})