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-polyfillsVerified import paths — ran on the pinned version, not inferred.
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.
Ensure `esbuild` is installed in your project: `npm install esbuild` or `yarn add esbuild`.
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.
When using `polyfillGlobals` or `inject` for `__filename` and `__dirname`, explicitly configure their desired string values via the options object to match your deployment context.
Install esbuild as a direct dependency: `npm install esbuild` or `yarn add esbuild`.
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.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.