JSPM

node-stdlib-browser

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 922496
  • Score
    100M100P100Q181974F
  • License MIT

Node standard library for browser.

Package Exports

  • node-stdlib-browser
  • node-stdlib-browser/mock/buffer
  • node-stdlib-browser/mock/console
  • node-stdlib-browser/mock/dns
  • node-stdlib-browser/mock/empty
  • node-stdlib-browser/mock/net
  • node-stdlib-browser/mock/process
  • node-stdlib-browser/mock/punycode
  • node-stdlib-browser/mock/tls
  • node-stdlib-browser/mock/tty
  • node-stdlib-browser/package.json

Readme

node-stdlib-browser

Build Status

Node standard library for browser.

Features:

  • Based on node-libs-browser for Webpack
  • Maintained with newer versions and modern implementations
  • Works with Webpack and Rollup, 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.

// webpack.config.js
const stdLibBrowser = require('node-stdlib-browser');
const webpack = require('webpack');

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

Some packages such as native-url expose ESM file through .mjs extension. Additional Webpack configuration could be needed to properly handle those packages.

For example, to make native-url use ESM version of native-querystring, apply following configuration:

// webpack.config.js

module.exports = {
    // ...
    module: {
        rules: [
            {
                type: 'javascript/auto',
                test: /\.mjs$/,
                include: /\/native-url\//,
                resolve: {
                    mainFields: ['module']
                }
            }
        ];
    }
}

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). Additionally, it’s recommended to handle Node globals automatically.

Some dependencies can have circular dependencies and Rollup will warn you about that. You can ignore these warnings with onwarn function.

// rollup.config.js
const stdLibBrowser = require('node-stdlib-browser');
const globals = require('rollup-plugin-node-globals');
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');

module.exports = {
    // ...
    plugins: [
        alias({
            entries: stdLibBrowser
        }),
        resolve({
            browser: true
        }),
        commonjs(),
        json(),
        globals()
    ],
    onwarn: (warning, rollupWarn) => {
        const packagesWithCircularDependencies = [
            'util/',
            'assert/',
            'readable-stream/',
            'crypto-browserify/'
        ];
        if (
            !(
                warning.code === 'CIRCULAR_DEPENDENCY' &&
                packagesWithCircularDependencies.some((modulePath) =>
                    warning.importer.includes(modulePath)
                )
            )
        ) {
            rollupWarn(warning);
        }
    }
};

Package contents

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

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ć