Modern Node Polyfills
modern-node-polyfills provides polyfills for Node.js native modules (such as `fs`, `buffer`, `process`) to enable their use in non-Node environments like browsers, Cloudflare Workers, and Deno. It leverages a collection of modern, lightweight polyfills sourced directly from JSPM's `jspm-core` project. The package is currently at version 1.0.0 and while it does not specify a fixed release cadence, it is actively maintained. Its key differentiators include its focused approach on modern polyfills specifically tailored for non-Node runtime compatibility, offering a practical solution for edge environments, and providing utility functions like `polyfillPath`, `polyfillContent`, `inject`, and `polyfillGlobals` for code transformation.
Common errors
-
Error: Cannot find module 'esbuild'
cause The `esbuild` peer dependency is missing from your project's `node_modules`.fixInstall esbuild as a direct dependency: `npm install esbuild` or `yarn add esbuild`. -
ERR_MODULE_NOT_FOUND or Cannot read properties of undefined (reading 'polyfillPath') when using require()
cause The package is primarily designed for ESM. Attempting to use CommonJS `require()` might fail in environments or bundlers not configured to handle it correctly for this library.fixMigrate your import statements to ESM: `import { polyfillPath } from 'modern-node-polyfills';`. Ensure your project's `package.json` specifies `"type": "module"` if running in Node.js, or your bundler is configured for ESM. -
ReferenceError: process is not defined (or Buffer is not defined, global is not defined)
cause Your code is attempting to access Node.js global variables that have not been polyfilled or injected into the environment.fixUtilize the `polyfillGlobals` function to automatically replace these expressions in your code before execution, or use the `inject` function for more granular control over specific globals.
Warnings
- gotcha This package requires `esbuild` as a peer dependency, but it's not automatically installed. Failure to install `esbuild` will lead to runtime errors.
- gotcha modern-node-polyfills is designed to provide *subset* polyfills for Node.js native modules in non-Node environments (e.g., browsers, Cloudflare Workers, Deno). It does not aim for full Node.js API parity for all modules or their deeper functionalities.
- gotcha Polyfills for `__filename` and `__dirname` are static or user-configurable, not dynamically resolved based on the current file path like in a true Node.js environment.
Install
-
npm install modern-node-polyfills -
yarn add modern-node-polyfills -
pnpm add modern-node-polyfills
Imports
- polyfillPath
const { polyfillPath } = require('modern-node-polyfills')import { polyfillPath } from 'modern-node-polyfills' - inject
import inject from 'modern-node-polyfills'
import { inject } from 'modern-node-polyfills' - polyfillGlobals
const polyfillGlobals = require('modern-node-polyfills').polyfillGlobalsimport { polyfillGlobals } from 'modern-node-polyfills'
Quickstart
import { polyfillGlobals } from 'modern-node-polyfills';
const browserCode = `
console.log(global);
console.log(process.env.NODE_ENV);
const buf = Buffer.from('hello');
console.log(buf.toString());
console.log(__filename);
console.log(__dirname);
`;
async function transformAndLog() {
try {
const polyfilledContent = await polyfillGlobals(
browserCode,
{
__filename: '/app/index.js',
__dirname: '/app'
}
);
console.log('--- Original Code ---\n' + browserCode);
console.log('\n--- Polyfilled Content ---\n' + polyfilledContent);
// In a real browser/edge environment, you would now execute polyfilledContent
} catch (error) {
console.error('Error during polyfill:', error);
}
}
transformAndLog();