Socket
Socket
Sign inDemoInstall

node-stdlib-browser

Package Overview
Dependencies
110
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-stdlib-browser

Node standard library for browser.


Version published
Weekly downloads
244K
increased by0.65%
Maintainers
1
Install size
7.95 MB
Created
Weekly downloads
 

Readme

Source

node-stdlib-browser

Build Status

Node standard library for browser.

Features:

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.

// webpack.config.js
const stdLibBrowser = require('node-stdlib-browser');
const {
	NodeProtocolUrlPlugin
} = require('node-stdlib-browser/helpers/webpack/plugin');
const webpack = require('webpack');

module.exports = {
	// ...
	resolve: {
		alias: stdLibBrowser
	},
	plugins: [
		new NodeProtocolUrlPlugin(),
		new webpack.ProvidePlugin({
			process: stdLibBrowser.process,
			Buffer: [stdLibBrowser.buffer, 'Buffer']
		})
	]
};

If you’re using ESM config, additional configuration is needed to handle unspecified extensions:

// webpack.config.js
module.exports = {
	// ...
	module: {
		rules: [
			{
				test: /\.m?js$/,
				resolve: {
					fullySpecified: false
				}
			}
		]
	}
};

Rollup

Show me

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).

// rollup.config.js
const stdLibBrowser = require('node-stdlib-browser');
const {
	handleCircularDependancyWarning
} = require('node-stdlib-browser/helpers/rollup/plugin');
const { default: resolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const alias = require('@rollup/plugin-alias');
const inject = require('@rollup/plugin-inject');

module.exports = {
	// ...
	plugins: [
		alias({
			entries: stdLibBrowser
		}),
		resolve({
			browser: true
		}),
		commonjs(),
		json(),
		inject({
			process: stdLibBrowser.process,
			Buffer: [stdLibBrowser.buffer, 'Buffer']
		})
	],
	onwarn: (warning, rollupWarn) => {
		handleCircularDependancyWarning(warning, rollupWarn);
	}
};

esbuild

Show me

Using esbuild requires you to use helper utilities and plugins.

const path = require('path');
const esbuild = require('esbuild');
const plugin = require('node-stdlib-browser/helpers/esbuild/plugin');
const stdLibBrowser = require('node-stdlib-browser');

(async () => {
	await esbuild.build({
		// ...
		inject: [require.resolve('node-stdlib-browser/helpers/esbuild/shim')],
		define: {
			global: 'global',
			process: 'process',
			Buffer: 'Buffer'
		},
		plugins: [plugin(stdLibBrowser)]
	});
})();

Browserify

Show me

Bundling ES modules is currently not supported natively in Browserify, but you can try using esmify or babelify for transforming to CommonJS first.

const fs = require('fs');
const path = require('path');
const browserify = require('browserify');
const aliasify = require('aliasify');
const stdLibBrowser = require('node-stdlib-browser');

const b = browserify(
	[
		/* ... */
	],
	{
		// ...
		transform: [[aliasify, { aliases: stdLibBrowser }]],
		insertGlobalVars: {
			process: () => {
				return `require('${stdLibBrowser.process}')`;
			},
			Buffer: () => {
				return `require('${stdLibBrowser.buffer}').Buffer`;
			}
		}
	}
);

Package contents

ModuleBrowser implementationMock implementationNotes
assertassert
bufferbufferbufferbuffer@5 for IE 11 support
child_process
cluster
consoleconsole-browserifyconsole
constantsconstants-browserify
cryptocrypto-browserify
dgram
dnsdns
domaindomain-browser
eventsevents
fsMocking fs
httpstream-http
httpshttps-browserify
module
netnet
osos-browserify
pathpath-browserify
processprocessprocess
punycodepunycodepunycode@1 for browser support
querystringquerystring-es3Contains additional exports from newer Node versions
readline
repl
streamstream-browserify
string_decoderstring_decoder
sysutil
timerstimers-browserify
timers/promisesisomorphic-timers-promises
tlstls
ttytty-browserifytty
urlnode-urlContains additional exports from newer Node versions (URL and URLSearchParams are not polyfilled)
utilutil
vmvm-browserify
zlibbrowserify-zlib
_stream_duplexreadable-stream
_stream_passthroughreadable-stream
_stream_readablereadable-stream
_stream_transformreadable-stream
_stream_writablereadable-stream

API

packages

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.

Tips

Mocking fs

fs package doesn’t return anything since there are many different ways you can implement file system functionality in browser.

Examples of implementations:

Node support

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.

Browser support

Minimum supported version should be Internet Explorer 11, but most modules support even Internet Explorer 9.

Types

You can use default @types/node types.

License

MIT © Ivan Nikolić

Keywords

FAQs

Last updated on 08 Dec 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc