Gathering detailed insights and metrics for node-stdlib-browser
Gathering detailed insights and metrics for node-stdlib-browser
Gathering detailed insights and metrics for node-stdlib-browser
Gathering detailed insights and metrics for node-stdlib-browser
npm install node-stdlib-browser
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
188 Stars
98 Commits
13 Forks
4 Watching
2 Branches
2 Contributors
Updated on 21 Nov 2024
Minified
Minified + Gzipped
JavaScript (94%)
HTML (6%)
Cumulative downloads
Total Downloads
Last day
-6.4%
88,466
Compared to previous day
Last week
-0.5%
464,036
Compared to previous week
Last month
19.1%
1,982,966
Compared to previous month
Last year
408.1%
15,034,242
Compared to previous year
27
44
Node standard library for browser.
Features:
node-libs-browser
for Webpacknode:
protocol which
allows for builtin modules to be referenced by valid absolute URL stringsCheck example to see how modules work in browser environment.
1npm install node-stdlib-browser --save-dev
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.
1// webpack.config.js 2const stdLibBrowser = require('node-stdlib-browser'); 3const { 4 NodeProtocolUrlPlugin 5} = require('node-stdlib-browser/helpers/webpack/plugin'); 6const webpack = require('webpack'); 7 8module.exports = { 9 // ... 10 resolve: { 11 alias: stdLibBrowser 12 }, 13 plugins: [ 14 new NodeProtocolUrlPlugin(), 15 new webpack.ProvidePlugin({ 16 process: stdLibBrowser.process, 17 Buffer: [stdLibBrowser.buffer, 'Buffer'] 18 }) 19 ] 20};
If you’re using ESM config, additional configuration is needed to handle unspecified extensions:
1// webpack.config.js 2module.exports = { 3 // ... 4 module: { 5 rules: [ 6 { 7 test: /\.m?js$/, 8 resolve: { 9 fullySpecified: false 10 } 11 } 12 ] 13 } 14};
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).
1// rollup.config.js 2const stdLibBrowser = require('node-stdlib-browser'); 3const { 4 handleCircularDependancyWarning 5} = require('node-stdlib-browser/helpers/rollup/plugin'); 6const { default: resolve } = require('@rollup/plugin-node-resolve'); 7const commonjs = require('@rollup/plugin-commonjs'); 8const json = require('@rollup/plugin-json'); 9const alias = require('@rollup/plugin-alias'); 10const inject = require('@rollup/plugin-inject'); 11 12module.exports = { 13 // ... 14 plugins: [ 15 alias({ 16 entries: stdLibBrowser 17 }), 18 resolve({ 19 browser: true 20 }), 21 commonjs(), 22 json(), 23 inject({ 24 process: stdLibBrowser.process, 25 Buffer: [stdLibBrowser.buffer, 'Buffer'] 26 }) 27 ], 28 onwarn: (warning, rollupWarn) => { 29 handleCircularDependancyWarning(warning, rollupWarn); 30 } 31};
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.
1const inject = require('@rollup/plugin-inject'); 2 3const esbuildShim = require.resolve('node-stdlib-browser/helpers/esbuild/shim'); 4 5module.exports = async () => { 6 const { default: stdLibBrowser } = await import('node-stdlib-browser'); 7 return { 8 resolve: { 9 alias: stdLibBrowser 10 }, 11 optimizeDeps: { 12 include: ['buffer', 'process'] 13 }, 14 plugins: [ 15 { 16 ...inject({ 17 global: [esbuildShim, 'global'], 18 process: [esbuildShim, 'process'], 19 Buffer: [esbuildShim, 'Buffer'] 20 }), 21 enforce: 'post' 22 } 23 ] 24 }; 25};
If you wish to use simpler configuration, you can use one of the available Vite plugins which use this package under the hood:
Using esbuild requires you to use helper utilities and plugins.
1const path = require('path'); 2const esbuild = require('esbuild'); 3const plugin = require('node-stdlib-browser/helpers/esbuild/plugin'); 4const stdLibBrowser = require('node-stdlib-browser'); 5 6(async () => { 7 await esbuild.build({ 8 // ... 9 inject: [require.resolve('node-stdlib-browser/helpers/esbuild/shim')], 10 define: { 11 global: 'global', 12 process: 'process', 13 Buffer: 'Buffer' 14 }, 15 plugins: [plugin(stdLibBrowser)] 16 }); 17})();
Bundling ES modules is currently not supported natively in Browserify, but you can try using esmify or babelify for transforming to CommonJS first.
1const fs = require('fs'); 2const path = require('path'); 3const browserify = require('browserify'); 4const aliasify = require('aliasify'); 5const stdLibBrowser = require('node-stdlib-browser'); 6 7const b = browserify( 8 [ 9 /* ... */ 10 ], 11 { 12 // ... 13 transform: [[aliasify, { aliases: stdLibBrowser }]], 14 insertGlobalVars: { 15 process: () => { 16 return `require('${stdLibBrowser.process}')`; 17 }, 18 Buffer: () => { 19 return `require('${stdLibBrowser.buffer}').Buffer`; 20 } 21 } 22 } 23);
Module | Browser implementation | Mock implementation | Notes |
---|---|---|---|
assert | assert | ||
buffer | buffer | buffer | buffer@5 for IE 11 support |
child_process | |||
cluster | |||
console | console-browserify | console | |
constants | constants-browserify | ||
crypto | crypto-browserify | ||
dgram | |||
dns | dns | ||
domain | domain-browser | ||
events | events | ||
fs | Mocking fs | ||
http | stream-http | ||
https | https-browserify | ||
module | |||
net | net | ||
os | os-browserify | ||
path | path-browserify | ||
process | process | process | Contains additional exports from newer Node |
punycode | punycode | punycode@1 for browser support | |
querystring | querystring-es3 | Contains additional exports from newer Node versions | |
readline | |||
repl | |||
stream | stream-browserify | ||
string_decoder | string_decoder | ||
sys | util | ||
timers | timers-browserify | ||
timers/promises | isomorphic-timers-promises | ||
tls | tls | ||
tty | tty-browserify | tty | |
url | node-url | Contains additional exports from newer Node versions (URL and URLSearchParams are not polyfilled) | |
util | util | ||
vm | vm-browserify | ||
zlib | browserify-zlib | ||
_stream_duplex | readable-stream | ||
_stream_passthrough | readable-stream | ||
_stream_readable | readable-stream | ||
_stream_transform | readable-stream | ||
_stream_writable | readable-stream |
Returns: object
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.
fs
fs
package doesn’t return anything since there are many different ways you can
implement file system functionality in browser.
Examples of implementations:
Minimum supported version should be Node 10.
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.
Minimum supported version should be Internet Explorer 11, but most modules support even Internet Explorer 9.
You can use default @types/node
types.
MIT © Ivan Nikolić
No vulnerabilities found.
No security vulnerabilities found.