Maintained with newer versions and modern implementations
Works with Webpack, Rollup, Vite, esbuild and Browserify, but should also
work with other bundlers
Exports implementation with node: protocol which
allows for builtin modules to be referenced by valid absolute URL strings
Check example to see how modules work in browser environment.
Install
npm install node-stdlib-browser --save-dev
Usage
Webpack
Show me
As of Webpack 5, aliases and globals provider need to be explicitly configured.
If you want to handle node: protocol imports, you
need to provide helper plugin.
Since many packages expose only CommonJS implementation, you need to apply
plugins to handle CommonJS exports. Those packages could have dependencies
installed with npm so they need to be properly resolved (taking into account
browser-specific implementations).
Some dependencies can have circular dependencies and Rollup will warn you about
that. You can ignore these warnings with helper function
(reference).
Vite config uses combination of Rollup and esbuild plugins. It’s important
to use dynamic import when using CommonJS configuration so ESM version of
modules is picked up. This allows Vite bundling to use our mocking
implementation and implement heuristics such as proper tree-shaking and dead
code removal marking.
Exports absolute paths to each module directory (where package.json is
located), keyed by module names. Modules without browser replacements return
module with default export null.
Some modules have mocks in the mock directory. These are replacements with
minimal functionality.
Tips
Mocking fs
fs package doesn’t return anything since there are many different ways you can
implement file system functionality in browser.
If you’re using ESM in Node < 12.20, note that
subpath patterns
are not supported so mocks can’t be handled. In that case, it’s recommended to
use CommonJS implementation.
Browser support
Minimum supported version should be Internet Explorer 11, but most modules
support even Internet Explorer 9.