Registry / web-framework / node-stdlib-browser

node-stdlib-browser

JSON →
library1.3.1jsnpmunverified

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.

npm install node-stdlib-browser
INSTALL
IMPORT
SIG · NODE-STDLIB-BROWSE
N
node-stdlib-browser
web-frameworkjavascriptv1.3.1
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.

stdLibBrowser
import stdLibBrowser from 'node-stdlib-browser'; // or const stdLibBrowser = require('node-stdlib-browser');
import { stdLibBrowser } from 'node-stdlib-browser';
The main export is a default object containing aliases. Use `import stdLibBrowser from ...` for ESM or `const stdLibBrowser = require(...)` for CJS.
NodeProtocolUrlPlugin
import { NodeProtocolUrlPlugin } from 'node-stdlib-browser/helpers/webpack/plugin';
import NodeProtocolUrlPlugin from 'node-stdlib-browser/helpers/webpack/plugin';
Webpack-specific helper for `node:` protocol imports is a named export from a subpath.
handleCircularDependancyWarning
import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin';
const handleCircularDependancyWarning = require('node-stdlib-browser/helpers/rollup/plugin').handleCircularDependancyWarning;
Rollup-specific helper to suppress circular dependency warnings is a named export.
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';
Used for injecting global polyfills in esbuild and Vite configurations; often resolved via `require.resolve`.

This quickstart demonstrates how to configure Vite to use `node-stdlib-browser` for polyfilling Node.js built-in modules in a browser environment. It sets up aliases and injects global variables like `process` and `Buffer` using `@rollup/plugin-inject`.

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', }, ], }; };
Debug
Known issues
breakingWebpack 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`.
fix
Refer to the package's documentation for Webpack configuration, ensuring `resolve.alias` and `webpack.ProvidePlugin` are correctly set up, along with `NodeProtocolUrlPlugin` for `node:` protocol imports.
affects: >=1.0.0
gotchaWhen 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.
fix
Add a rule to your `webpack.config.js` to disable `fullySpecified` for JavaScript modules: `module: { rules: [{ test: /\.m?js$/, resolve: { fullySpecified: false } }] }`.
affects: >=1.0.0
gotchaRollup 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.
fix
Utilize the `handleCircularDependancyWarning` helper function from `node-stdlib-browser/helpers/rollup/plugin` in your Rollup `onwarn` hook to filter or ignore these specific warnings.
affects: >=1.0.0
gotchaWhen 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'.
fix
Ensure your Vite configuration uses `await import('node-stdlib-browser')` and sets `enforce: 'post'` on the `@rollup/plugin-inject` configuration.
affects: >=1.2.1
Errors
Common errors & fixes
ReferenceError: process is not defined
The `process` global or `Buffer` global (among others) is not automatically polyfilled in the browser environment without proper bundler configuration.
fix
For 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)
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.
fix
Verify 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"
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.
fix
This 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
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.
fix
Ensure 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.
Upgrade
Version history
1.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources