Node Standard Library for Browser
node-stdlib-browser provides polyfills for Node.js standard library modules, enabling code that relies on these Node.js built-ins to run in browser environments. It serves as a actively maintained alternative to the original `node-libs-browser` package, which is deprecated. Version 1.3.1 is the current stable release. The package does not adhere to a strict time-based release cadence but sees regular updates for dependency bumps, bug fixes, and new bundler support. Key differentiators include explicit support for modern bundlers like Webpack, Rollup, Vite, and esbuild, as well as handling the `node:` protocol for built-in module imports.
Common errors
-
ReferenceError: process is not defined
cause The `process` global or `Buffer` global (among others) is not automatically polyfilled in the browser environment without proper bundler configuration.fixFor Webpack, ensure `webpack.ProvidePlugin` is configured to map `process` and `Buffer`. For Rollup/Vite/esbuild, ensure an `inject` plugin (e.g., `@rollup/plugin-inject`) is set up to provide these globals, typically pointing to `node-stdlib-browser`'s implementations. -
Module not found: Error: Can't resolve 'fs' in '...' (or other Node.js built-in modules)
cause Your bundler is unable to resolve an import for a Node.js standard library module that is being used in a dependency, because the necessary alias or polyfill configuration for `node-stdlib-browser` is missing or incorrect.fixVerify that your bundler's configuration (e.g., `webpack.config.js`, `rollup.config.js`, `vite.config.js`) includes `node-stdlib-browser` in its `resolve.alias` section. -
RollupError: "createRequire" is not exported by "node_modules/node-stdlib-browser/esm/mock/empty.js"
cause This error can occur in Vite/Rollup builds when a dependency attempts to use `createRequire` or other Node.js-specific features from a mocked module provided by `node-stdlib-browser`, and the bundler's configuration for handling ESM/CJS interop or protocol imports is insufficient.fixThis often points to a larger issue with `node:` protocol imports or mixed ESM/CJS modules. Consider using `vite-plugin-node-polyfills` or carefully reviewing the `node-stdlib-browser` documentation for Vite regarding dynamic imports and plugin enforcement. You might also need to explicitly mark certain modules as external in your bundler configuration if they are causing issues. -
The injected path ".../node_modules/node-stdlib-browser/helpers/esbuild/shim.js" cannot be marked as external
cause This issue can arise when esbuild or Vite (which uses esbuild) attempts to mark the shim file for `node-stdlib-browser` as external, but it needs to be bundled and injected.fixEnsure that the esbuild `inject` configuration correctly references the shim, and that no other configurations are inadvertently marking it as external. This might involve specific plugin order or configuration details depending on your bundler version.
Warnings
- breaking Webpack 5 requires explicit configuration for aliases and global providers. Users migrating from Webpack 4 or earlier will need to update their `webpack.config.js` to include `resolve.alias` and `plugins` for `NodeProtocolUrlPlugin` and `webpack.ProvidePlugin`.
- gotcha When using ESM configuration in Webpack, specifically for handling unspecified extensions, you might encounter module resolution issues. This often manifests when importing CommonJS modules into an ESM context.
- gotcha Rollup may emit warnings about circular dependencies when bundling packages that utilize Node.js built-ins. This is a common occurrence in the Node.js ecosystem, and `node-stdlib-browser` provides a helper to manage these warnings.
- gotcha When configuring Vite, it is crucial to use dynamic `import()` for `node-stdlib-browser` if your Vite config file is in CommonJS format, to ensure the ESM version of the modules is picked up for proper tree-shaking and dead code removal. The `inject` plugin should also be enforced as 'post'.
Install
-
npm install node-stdlib-browser -
yarn add node-stdlib-browser -
pnpm add node-stdlib-browser
Imports
- stdLibBrowser
import { stdLibBrowser } from 'node-stdlib-browser';import stdLibBrowser from 'node-stdlib-browser'; // or const stdLibBrowser = require('node-stdlib-browser'); - NodeProtocolUrlPlugin
import NodeProtocolUrlPlugin from 'node-stdlib-browser/helpers/webpack/plugin';
import { NodeProtocolUrlPlugin } from 'node-stdlib-browser/helpers/webpack/plugin'; - handleCircularDependancyWarning
const handleCircularDependancyWarning = require('node-stdlib-browser/helpers/rollup/plugin').handleCircularDependancyWarning;import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'; - esbuildShim
const esbuildShim = require.resolve('node-stdlib-browser/helpers/esbuild/shim'); // For direct import if your bundler supports it: // import esbuildShim from 'node-stdlib-browser/helpers/esbuild/shim';
Quickstart
import { defineConfig } from 'vite';
import inject from '@rollup/plugin-inject';
const esbuildShim = require.resolve('node-stdlib-browser/helpers/esbuild/shim');
export default async () => {
const { default: stdLibBrowser } = await import('node-stdlib-browser');
return {
resolve: {
alias: stdLibBrowser,
},
optimizeDeps: {
include: ['buffer', 'process'],
},
plugins: [
{
...inject({
global: [esbuildShim, 'global'],
process: [esbuildShim, 'process'],
Buffer: [esbuildShim, 'Buffer'],
}),
enforce: 'post',
},
],
};
};