Node.js Core Libraries for Browsers
This package, `node-libs-browser-okam` (a fork of the original `node-libs-browser`), provides browser-compatible polyfills and mock implementations for a wide range of Node.js core modules such as `buffer`, `process`, `path`, `events`, and `util`. Currently at version `2.2.5`, it is explicitly marked as deprecated and only accepts bug fixes, with no new features or breaking changes planned. Its primary use case is within module bundlers like Webpack to automatically resolve Node.js built-in module imports to browser-compatible alternatives, preventing "module not found" errors in front-end applications. The package exports a JavaScript object where keys are Node.js module names and values are the absolute paths to their respective browser implementations or `null` if no browser-compatible version is available. It relies on older versions of some key dependencies like `buffer` and `punycode` to maintain broader browser compatibility (e.g., IE9).
Common errors
-
Module not found: Error: Can't resolve 'fs' in '/path/to/your/project'
cause Attempting to import a Node.js core module (like `fs`, `net`, `child_process`) that `node-libs-browser-okam` does not provide a browser polyfill for (its entry in the exported object is `null`).fixRemove the problematic import from your browser-side code. These modules are fundamentally tied to the Node.js environment and typically cannot be replicated in a browser. Re-evaluate your architecture if such functionality is critical for the client. -
TypeError: Buffer.from is not a function
cause Code expects the `Buffer.from()` static method, but `node-libs-browser-okam` uses an older `feross/buffer@4.x` polyfill which predates this API, primarily supporting `new Buffer()`.fixRefactor your code to use `new Buffer(array)` or `new Buffer(string, encoding)` instead of `Buffer.from()`. Alternatively, you can configure your bundler to alias `buffer` to a different, more modern polyfill that supports `Buffer.from()`. -
ReferenceError: process is not defined
cause The global `process` object, expected by some Node.js modules, is not correctly aliased to its browser polyfill by the bundler, or accessed in a context where the polyfill isn't available.fixEnsure your bundler (e.g., Webpack, Rollup, Browserify) has `node-libs-browser-okam` or a similar polyfill configured to correctly alias the global `process` object. For Webpack, this often involves configuring `resolve.fallback` or `ProvidePlugin` for older versions. -
Uncaught TypeError: crypto.createHash is not a function
cause While `crypto` has a browser polyfill, it may not implement all functions available in Node.js's native `crypto` module, leading to runtime errors for unsupported APIs.fixReview the specific `crypto` function being called. If it's not implemented, you'll need to find a dedicated browser-compatible cryptography library that provides the required functionality, or refactor your code to avoid the unsupported API.
Warnings
- deprecated The `node-libs-browser` project is officially deprecated and only accepts bug fixes. No new features or breaking changes will be introduced. Users should consider modern alternatives for polyfilling Node.js built-ins in browser environments, or ensure their bundler explicitly handles these.
- gotcha The `buffer` implementation uses `feross/buffer@4.x` instead of the newer `5.x` version. This is due to a deliberate choice to support older browsers (like IE9) by avoiding reliance on typed arrays. Consequently, `Buffer.from()` and other modern `Buffer` static methods are not available.
- gotcha The `punycode` implementation uses `bestiejs/punycode.js@1.x`, which requires older JavaScript engines. This means it may not be compatible with newer `punycode` APIs or expectations of modern JavaScript syntax.
- gotcha Many Node.js core modules, such as `child_process`, `cluster`, `dgram`, `fs`, `module`, `net`, `readline`, `repl`, `tls`, do not have browser-compatible implementations within this package. Their entries in the exported object are `null`.
Install
-
npm install node-libs-browser-okam -
yarn add node-libs-browser-okam -
pnpm add node-libs-browser-okam
Imports
- nodeLibs
import { nodeLibs } from 'node-libs-browser-okam';import nodeLibs from 'node-libs-browser-okam';
- nodeLibs
const nodeLibs = require('node-libs-browser-okam'); - bufferPath
import { buffer } from 'node-libs-browser-okam';const bufferPath = require('node-libs-browser-okam').buffer;
Quickstart
import nodeLibs from 'node-libs-browser-okam';
console.log('Available Node.js browser libs:');
for (const libName in nodeLibs) {
const libPath = nodeLibs[libName];
if (libPath) {
console.log(`- ${libName}: ${libPath}`);
} else {
console.log(`- ${libName}: (No browser implementation available)`);
}
}
// Example: How a bundler might use it to resolve 'buffer'
const resolvedBufferPath = nodeLibs.buffer;
if (resolvedBufferPath) {
console.log(`\nPath to buffer polyfill: ${resolvedBufferPath}`);
} else {
console.log('\nBuffer polyfill not found.');
}
// This package is primarily used by bundlers like Webpack for automatic aliasing.
// For direct usage, you would typically configure a bundler to use these paths.