{"id":11457,"library":"node-stdlib-browser","title":"Node Standard Library for Browser","description":"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.","status":"active","version":"1.3.1","language":"javascript","source_language":"en","source_url":"https://github.com/niksy/node-stdlib-browser","tags":["javascript","node","std","browser","api","typescript"],"install":[{"cmd":"npm install node-stdlib-browser","lang":"bash","label":"npm"},{"cmd":"yarn add node-stdlib-browser","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-stdlib-browser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The main export is a default object containing aliases. Use `import stdLibBrowser from ...` for ESM or `const stdLibBrowser = require(...)` for CJS.","wrong":"import { stdLibBrowser } from 'node-stdlib-browser';","symbol":"stdLibBrowser","correct":"import stdLibBrowser from 'node-stdlib-browser';\n// or\nconst stdLibBrowser = require('node-stdlib-browser');"},{"note":"Webpack-specific helper for `node:` protocol imports is a named export from a subpath.","wrong":"import NodeProtocolUrlPlugin from 'node-stdlib-browser/helpers/webpack/plugin';","symbol":"NodeProtocolUrlPlugin","correct":"import { NodeProtocolUrlPlugin } from 'node-stdlib-browser/helpers/webpack/plugin';"},{"note":"Rollup-specific helper to suppress circular dependency warnings is a named export.","wrong":"const handleCircularDependancyWarning = require('node-stdlib-browser/helpers/rollup/plugin').handleCircularDependancyWarning;","symbol":"handleCircularDependancyWarning","correct":"import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin';"},{"note":"Used for injecting global polyfills in esbuild and Vite configurations; often resolved via `require.resolve`.","symbol":"esbuildShim","correct":"const esbuildShim = require.resolve('node-stdlib-browser/helpers/esbuild/shim');\n// For direct import if your bundler supports it:\n// import esbuildShim from 'node-stdlib-browser/helpers/esbuild/shim';"}],"quickstart":{"code":"import { defineConfig } from 'vite';\nimport inject from '@rollup/plugin-inject';\n\nconst esbuildShim = require.resolve('node-stdlib-browser/helpers/esbuild/shim');\n\nexport default async () => {\n  const { default: stdLibBrowser } = await import('node-stdlib-browser');\n  return {\n    resolve: {\n      alias: stdLibBrowser,\n    },\n    optimizeDeps: {\n      include: ['buffer', 'process'],\n    },\n    plugins: [\n      {\n        ...inject({\n          global: [esbuildShim, 'global'],\n          process: [esbuildShim, 'process'],\n          Buffer: [esbuildShim, 'Buffer'],\n        }),\n        enforce: 'post',\n      },\n    ],\n  };\n};","lang":"typescript","description":"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`."},"warnings":[{"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.","message":"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`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Add a rule to your `webpack.config.js` to disable `fullySpecified` for JavaScript modules: `module: { rules: [{ test: /\\.m?js$/, resolve: { fullySpecified: false } }] }`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your Vite configuration uses `await import('node-stdlib-browser')` and sets `enforce: 'post'` on the `@rollup/plugin-inject` configuration.","message":"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'.","severity":"gotcha","affected_versions":">=1.2.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"The `process` global or `Buffer` global (among others) is not automatically polyfilled in the browser environment without proper bundler configuration.","error":"ReferenceError: process is not defined"},{"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.","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.","error":"Module not found: Error: Can't resolve 'fs' in '...' (or other Node.js built-in modules)"},{"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.","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.","error":"RollupError: \"createRequire\" is not exported by \"node_modules/node-stdlib-browser/esm/mock/empty.js\""},{"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.","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.","error":"The injected path \".../node_modules/node-stdlib-browser/helpers/esbuild/shim.js\" cannot be marked as external"}],"ecosystem":"npm"}