Rollup Plugin for Node.js Built-ins
This Rollup plugin shims Node.js built-in modules, enabling projects designed for a Node.js environment or Browserify to run in a browser bundle. It allows `require` or `import` statements for modules like `events`, `util`, `path`, and `buffer` to be resolved during the Rollup bundling process. The latest stable version is 2.1.2, released in 2017. Due to its age, it is no longer actively maintained and may have compatibility issues with newer Rollup versions or modern JavaScript features. For many built-ins (marked with an asterisk in the README), it requires the companion `rollup-plugin-node-globals` for proper functionality, especially for `process` and `Buffer`. Key differentiators at the time of its release were its ability to provide Browserify-compatible shims within the Rollup ecosystem, bridging a gap for projects with existing Node.js module dependencies. However, more modern alternatives like `@rollup/plugin-node-resolve` (with specific configuration) or `rollup-plugin-polyfill-node` are now generally recommended.
Common errors
-
(!) Missing Node.js built-in module 'punycode' (or similar built-in) ... You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins
cause A dependency in your project or your own code is trying to import a Node.js built-in module, but `rollup-plugin-node-builtins` is either not installed, not configured, or configured incorrectly (e.g., wrong order relative to `node-globals`).fixEnsure `rollup-plugin-node-builtins` is installed and added to your `rollup.config.js` plugins array. If the module is one that requires `rollup-plugin-node-globals`, ensure `globals()` is before `builtins()`. -
ReferenceError: process is not defined
cause A module in your bundle expects the global `process` object (a Node.js global), but `rollup-plugin-node-globals` (which provides shims for globals like `process` and `Buffer`) is not included or is incorrectly ordered.fixInstall `rollup-plugin-node-globals` and add `globals()` to your Rollup plugins array, ensuring it appears *before* `builtins()`. -
TypeError: Cannot read properties of undefined (reading 'randomBytes')
cause Attempted to use the `crypto` module without explicitly enabling it in `rollup-plugin-node-builtins` options.fixEnable the `crypto` shim by configuring the plugin as `builtins({ crypto: true })`.
Warnings
- breaking This package is unmaintained. The last update was in 2017 (v2.1.2). It may not be compatible with newer Rollup versions (e.g., Rollup 3+) or modern JavaScript syntax/features like dynamic imports, potentially causing build failures or runtime errors.
- gotcha Many Node.js built-in modules (e.g., `process`, `stream`, `http`, `buffer`) require `rollup-plugin-node-globals` to be used *before* `rollup-plugin-node-builtins` in the Rollup plugin array. Failing to include or correctly order `node-globals` will result in unresolved references (`process is not defined`) for modules depending on global Node variables.
- gotcha Some Node.js built-in modules are not fully shimmed or have limitations. For example, `vm` has fewer corner cases handled, `http` and `https` are the same (no protocol differentiation), and modules like `dns`, `dgram`, `child_process`, `cluster`, `module`, `net`, `readline`, `repl`, and `tls` only return mocks, making them largely unusable.
- gotcha The `crypto` and `fs` modules are *optional* and require explicit configuration (`{crypto: true}` or `{fs: true}`) to be enabled. Without this, attempts to `import` or `require` them will likely fail or use a basic, potentially non-functional CommonJS shim from Browserify.
- gotcha Some shimmed modules, particularly `streams` and anything that depends on them (like `http`), have complex circular references which significantly hinder Rollup's tree-shaking capabilities, leading to larger-than-expected bundle sizes. `url` methods also don't tree-shake well.
Install
-
npm install rollup-plugin-node-builtins -
yarn add rollup-plugin-node-builtins -
pnpm add rollup-plugin-node-builtins
Imports
- builtins
const builtins = require('rollup-plugin-node-builtins');import builtins from 'rollup-plugin-node-builtins';
- EventEmitter
const EventEmitter = require('events');import EventEmitter from 'events';
- inherits
import util from 'util'; const { inherits } = util;import { inherits } from 'util';
Quickstart
import rollup from 'rollup';
import builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
async function build() {
const bundle = await rollup.rollup({
input: 'main.js',
plugins: [
globals(), // Must come before builtins for proper context setup
builtins() // Enables Node.js built-ins
]
});
await bundle.write({
file: 'bundle.js',
format: 'iife',
name: 'MyBundle',
sourcemap: true
});
console.log('Rollup build complete!');
}
// Example main.js content for context (not part of quickstart code itself)
// import * as http from 'http';
// http.request('http://example.com');
// import { EventEmitter } from 'events';
// const emitter = new EventEmitter();
build().catch(console.error);