Node.js Core Libraries for Browser Environments
node-libs-browser is a utility package providing browser-compatible polyfills and mock implementations for a subset of Node.js core modules. Its primary purpose is to enable bundlers like Webpack to resolve `require('fs')` or `require('buffer')` in browser environments, substituting Node.js-specific APIs with browser-friendly alternatives. The current stable version is 2.2.1. This library is officially deprecated, meaning it will not accept new features or breaking changes; only bugfixes. Its release cadence is effectively stalled, with the last major update several years ago. While historically critical for enabling Node.js code in the browser, modern development often favors more granular polyfills, conditional imports, or alternatives like `node-stdlib-browser` that are actively maintained and offer newer implementations and ESM support, addressing issues like `punycode` deprecation in newer Node.js versions.
Common errors
-
Module not found: Error: Can't resolve 'fs' in 'your-module-path'
cause An application or one of its dependencies is attempting to `require('fs')` (or another Node.js-specific module) in a browser environment, and `node-libs-browser` does not provide a browser implementation or mock for it (its value is `null`).fixReview the usage of Node.js core modules. For modules with `null` implementations, refactor the code to avoid them in browser contexts, or use a specific, actively maintained browser polyfill if available and appropriate, configuring your bundler to alias it. -
Uncaught ReferenceError: process is not defined
cause Code running in the browser expects the global `process` object (a Node.js global), but the `process` polyfill provided by `node-libs-browser` or a similar solution has not been correctly applied by the bundler.fixEnsure your bundler's configuration (e.g., Webpack's `NodeSourcePlugin`, `node` option, or `ProvidePlugin`) correctly sets up the `process` polyfill. Verify that `node-libs-browser` (or its `process` sub-dependency) is included and active. -
Buffer.from is not a function
cause The `buffer` polyfill being used (often `feross/buffer@4.x` from `node-libs-browser`) is an older version that predates the `Buffer.from` and `Buffer.alloc` APIs introduced in Node.js v6.fixIf your application requires modern `Buffer` APIs, you must explicitly configure your bundler to use a newer `buffer` polyfill (e.g., `feross/buffer@5.x` or later) and accept potential compatibility issues with very old browsers like IE9. -
(node:XXXX) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
cause Your Node.js environment is v21 or newer, and `node-libs-browser` (or a dependency transitively) is using the deprecated bundled `punycode` module.fixRun `npm ls punycode` to identify the direct or transitive dependency. If `node-libs-browser` is the culprit, consider migrating to a more modern alternative like `node-stdlib-browser` which addresses this, or use bundler `overrides` to force a newer, userland `punycode.js` version.
Warnings
- deprecated This library is officially deprecated and is in maintenance mode. It will not receive new features or breaking changes, only critical bug fixes. Users should consider modern alternatives like `node-stdlib-browser` for active development and better compatibility with newer Node.js versions and ESM.
- gotcha The `buffer` implementation (`feross/buffer@4.x`) is outdated and does not support features reliant on Typed Arrays found in `feross/buffer@5.x`. This decision was made to maintain compatibility with older browsers like IE9.
- gotcha The `punycode` implementation (`bestiejs/punycode.js@1.x`) is outdated and does not support modern JavaScript engines (requiring `const` and `let` for newer versions). The Node.js `punycode` module itself is also deprecated since Node.js v21/22.
- gotcha Many Node.js core modules (e.g., `child_process`, `cluster`, `fs`, `net`, `tls`) are explicitly `null` in `node-libs-browser`, meaning no browser implementation or mock is provided. Code attempting to use these will fail in the browser.
- gotcha This package is primarily a low-level utility for bundlers like Webpack. Direct integration into application logic for polyfilling purposes is generally discouraged. Improper configuration or direct usage can lead to unexpected behavior or larger bundle sizes.
Install
-
npm install node-libs-browser -
yarn add node-libs-browser -
pnpm add node-libs-browser
Imports
- nodeLibs
import nodeLibs from 'node-libs-browser';
const nodeLibs = require('node-libs-browser'); - bufferPath
import { buffer } from 'node-libs-browser';const bufferPath = require('node-libs-browser').buffer; - fsModule
require('node-libs-browser/fs');const fsModule = require('node-libs-browser').fs;
Quickstart
import nodeLibs from 'node-libs-browser';
console.log('Available Node.js core library browser implementations:');
for (const libName in nodeLibs) {
const implementationPath = nodeLibs[libName];
if (implementationPath) {
console.log(`- ${libName}: ${implementationPath} (Browser implementation/mock available)`);
} else {
console.log(`- ${libName}: (No browser implementation or mock provided by node-libs-browser)`);
}
}
// This library is primarily used internally by bundlers like Webpack.
// For example, Webpack's NodeSourcePlugin uses this mapping to resolve
// Node.js core module imports to browser-compatible versions or mocks.
// You typically don't directly import and use the paths in your application code.
// For instance, if your application code has 'require("path")',
// Webpack (using node-libs-browser) would internally map it to the path-browserify implementation.