Rollup Plugin for Node.js Polyfills (Legacy)
The `rollup-plugin-node-polyfills` package, currently at version `0.2.1` and last published over 7 years ago, is an abandoned Rollup plugin designed to provide browser-compatible polyfills or shims for Node.js built-in modules. Its primary function was to allow Rollup to bundle code containing `require()` or `import` statements for Node.js core modules (such as `events`, `path`, `buffer`, `http`, and `util`) when targeting a browser environment. The plugin leverages ES6 ports of Browserify modules for its polyfills, enabling both named and default imports for many modules. However, due to its inactive maintenance, developers are strongly advised to use modern, actively maintained alternatives like `rollup-plugin-polyfill-node` to ensure compatibility, security, and access to up-to-date polyfills. This legacy plugin has known limitations, including incomplete shims for certain modules, issues with tree-shaking for complex modules like `stream` and `http` due to circular references, and modules that only return mock objects instead of functional polyfills.
Common errors
-
Module not found: Can't resolve 'events'
cause A Node.js built-in module is being imported or required in a Rollup bundle without `rollup-plugin-node-polyfills` being configured or the specific polyfill failing.fixEnsure `rollup-plugin-node-polyfills` is correctly installed and added to your Rollup plugins array. If using a specific module like `crypto` or `fs`, ensure it's explicitly enabled via plugin options. Consider migrating to `rollup-plugin-polyfill-node`. -
TypeError: process.nextTick is not a function
cause Code relies on a `process` API (`process.nextTick`) that is either not fully polyfilled or the `process` polyfill isn't correctly applied.fixVerify the plugin is active and correctly processes files where `process` is used. For modern projects, it's recommended to use `rollup-plugin-polyfill-node` for better `process` polyfill support. -
Uncaught ReferenceError: Buffer is not defined
cause The `Buffer` global or import is not being correctly provided by the polyfill, or code is attempting to use `Buffer` without importing it explicitly.fixEnsure `import { Buffer } from 'buffer';` is present where `Buffer` is used. Verify the `buffer` polyfill is active through the plugin. Consider using a newer polyfill plugin if issues persist. -
TypeError: require is not a function
cause Your bundled code (intended for the browser) contains `require()` calls for Node.js modules, and the plugin failed to transform them into browser-compatible imports.fixConfirm the plugin is enabled and configured to process all relevant files (`include` option). For better reliability, especially in modern Rollup setups, use `import` statements over `require()` where possible and upgrade to `rollup-plugin-polyfill-node`.
Warnings
- breaking This package, `rollup-plugin-node-polyfills` (from Ionic Team), is abandoned and has not been updated since version `0.2.1` (last published June 2019). It is not actively maintained and may have unaddressed bugs or security vulnerabilities.
- gotcha The `crypto` module is not effectively shimmed by default and provides limited functionality, primarily from a browserify CommonJS shim.
- gotcha Modules such as `http`, `https`, and `stream` contain circular references, which can significantly hinder tree-shaking and lead to larger bundle sizes.
- gotcha Several Node.js modules (e.g., `dns`, `dgram`, `child_process`, `cluster`, `module`, `net`, `readline`, `repl`, `tls`) are not truly shimmed; they only return mock objects, rendering their functionality essentially unavailable in the browser environment.
- gotcha The `fs` (file system) module's browserified shim is optional and not included by default.
- gotcha The `vm` module has known limitations and does not support all corner cases, especially when used in web workers.
Install
-
npm install rollup-plugin-node-polyfills -
yarn add rollup-plugin-node-polyfills -
pnpm add rollup-plugin-node-polyfills
Imports
- nodePolyfills
const nodePolyfills = require('rollup-plugin-node-polyfills');import nodePolyfills from 'rollup-plugin-node-polyfills';
- EventEmitter
const EventEmitter = require('events');import EventEmitter from 'events';
- inherits
import util from 'util'; const inherits = util.inherits;
import { inherits } from 'util'; - Buffer
const Buffer = require('buffer').Buffer;import { Buffer } from 'buffer';
Quickstart
import nodePolyfills from 'rollup-plugin-node-polyfills';
import EventEmitter from 'events';
import { inherits } from 'util';
import { Buffer } from 'buffer';
const eventEmitter = new EventEmitter();
console.log(eventEmitter instanceof EventEmitter);
class MyClass {}
inherits(MyClass, EventEmitter);
const myInstance = new MyClass();
console.log(myInstance instanceof EventEmitter);
const buf = Buffer.from('hello');
console.log(buf.toString());
export default {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'esm'
},
plugins: [
nodePolyfills()
]
};