Registry / devops / modern-node-polyfills

modern-node-polyfills

JSON →
library1.0.0jsnpmunverified

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.

npm install modern-node-polyfills
INSTALL
IMPORT
SIG · MODERN-NODE-POLYFI
M
modern-node-polyfills
devopsjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

polyfillPath
import { polyfillPath } from 'modern-node-polyfills'
const { polyfillPath } = require('modern-node-polyfills')
The library primarily uses ESM `import` syntax. While some bundler configurations might support `require()`, it's not the officially recommended or explicitly supported import style.
inject
import { inject } from 'modern-node-polyfills'
import inject from 'modern-node-polyfills'
All public utilities are named exports; there is no default export. This function helps replace global expressions and module imports within code.
polyfillGlobals
import { polyfillGlobals } from 'modern-node-polyfills'
const polyfillGlobals = require('modern-node-polyfills').polyfillGlobals
This utility is crucial for automatically replacing common Node.js global expressions (like `process`, `Buffer`, `__filename`) with their polyfilled equivalents.

This quickstart demonstrates how to use `polyfillGlobals` to transform code containing Node.js globals (`process`, `Buffer`, `global`, `__filename`, `__dirname`) into a version suitable for non-Node environments, including custom paths for file-system specific globals.

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();
Debug
Known issues
gotchaThis package requires `esbuild` as a peer dependency, but it's not automatically installed. Failure to install `esbuild` will lead to runtime errors.
fix
Ensure `esbuild` is installed in your project: `npm install esbuild` or `yarn add esbuild`.
affects: >=1.0.0
gotchamodern-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.
fix
Review the list of supported modules and their features (linked in the README) to ensure the specific Node.js APIs your application uses are covered. Test thoroughly in your target environment.
affects: >=1.0.0
gotchaPolyfills 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.
fix
When using `polyfillGlobals` or `inject` for `__filename` and `__dirname`, explicitly configure their desired string values via the options object to match your deployment context.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'esbuild'
The `esbuild` peer dependency is missing from your project's `node_modules`.
fix
Install 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()
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.
fix
Migrate 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)
Your code is attempting to access Node.js global variables that have not been polyfilled or injected into the environment.
fix
Utilize 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.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
esbuildrequiredRequired as a peer dependency for internal bundling and parsing operations. It must be installed alongside modern-node-polyfills.
Agent activity
6 hits · last 30 days
node
6
Resources
modern-node-polyfills — npm install modern-node-polyfills · libregistry