{"id":11434,"library":"node-libs-browser","title":"Node.js Core Libraries for Browser Environments","description":"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.","status":"deprecated","version":"2.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/webpack/node-libs-browser","tags":["javascript"],"install":[{"cmd":"npm install node-libs-browser","lang":"bash","label":"npm"},{"cmd":"yarn add node-libs-browser","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-libs-browser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primarily used in CommonJS contexts (e.g., bundler configurations). While ESM imports may work in some setups, `require` is more historically accurate for this package's typical usage.","wrong":"import nodeLibs from 'node-libs-browser';","symbol":"nodeLibs","correct":"const nodeLibs = require('node-libs-browser');"},{"note":"The package exports an object where keys are module names and values are *paths* to their browser implementations or `null`, not named exports of the modules themselves. Attempting to destructure directly will fail.","wrong":"import { buffer } from 'node-libs-browser';","symbol":"bufferPath","correct":"const bufferPath = require('node-libs-browser').buffer;"},{"note":"Access individual module paths as properties of the imported object. Direct subpath imports (e.g., `require('node-libs-browser/fs')`) are not the intended API and will likely result in a module not found error.","wrong":"require('node-libs-browser/fs');","symbol":"fsModule","correct":"const fsModule = require('node-libs-browser').fs;"}],"quickstart":{"code":"import nodeLibs from 'node-libs-browser';\n\nconsole.log('Available Node.js core library browser implementations:');\n\nfor (const libName in nodeLibs) {\n  const implementationPath = nodeLibs[libName];\n  if (implementationPath) {\n    console.log(`- ${libName}: ${implementationPath} (Browser implementation/mock available)`);\n  } else {\n    console.log(`- ${libName}: (No browser implementation or mock provided by node-libs-browser)`);\n  }\n}\n\n// This library is primarily used internally by bundlers like Webpack.\n// For example, Webpack's NodeSourcePlugin uses this mapping to resolve\n// Node.js core module imports to browser-compatible versions or mocks.\n// You typically don't directly import and use the paths in your application code.\n// For instance, if your application code has 'require(\"path\")',\n// Webpack (using node-libs-browser) would internally map it to the path-browserify implementation.","lang":"javascript","description":"Demonstrates how to import `node-libs-browser` and iterate through the provided Node.js core library mappings, showing which modules have browser implementations or mocks."},"warnings":[{"fix":"Migrate to actively maintained alternatives or ensure your bundler configuration handles Node.js polyfills explicitly without relying solely on this package.","message":"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.","severity":"deprecated","affected_versions":">=2.2.1"},{"fix":"If modern `Buffer` API features are required and IE9 compatibility is not a concern, you may need to explicitly configure your bundler to use a newer `buffer` polyfill or an alternative package.","message":"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.","severity":"gotcha","affected_versions":">=2.2.0"},{"fix":"For projects running on newer Node.js versions, address `punycode` deprecation warnings by updating dependencies that might be transitively relying on it, or using an explicit userland `punycode.js` package and configuring bundler aliases.","message":"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.","severity":"gotcha","affected_versions":">=2.2.0"},{"fix":"Refactor application code to avoid Node.js-specific modules in browser contexts, use conditional imports for environment-specific code, or explicitly mock/polyfill unavailable modules if essential.","message":"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.","severity":"gotcha","affected_versions":"*"},{"fix":"Ensure `node-libs-browser` is configured via your bundler's options (e.g., Webpack's `resolve.alias` or `node` options) rather than being manually imported and consumed in application code. Consider bundler-specific plugins or alternatives that provide more opinionated or modern approaches to polyfilling.","message":"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.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Review 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.","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`).","error":"Module not found: Error: Can't resolve 'fs' in 'your-module-path'"},{"fix":"Ensure 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.","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.","error":"Uncaught ReferenceError: process is not defined"},{"fix":"If 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.","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.","error":"Buffer.from is not a function"},{"fix":"Run `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.","cause":"Your Node.js environment is v21 or newer, and `node-libs-browser` (or a dependency transitively) is using the deprecated bundled `punycode` module.","error":"(node:XXXX) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead."}],"ecosystem":"npm"}