{"id":15141,"library":"modern-node-polyfills","title":"Modern Node Polyfills","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/Aslemammad/modern-node-polyfills","tags":["javascript","polyfills","shims","edge","typescript"],"install":[{"cmd":"npm install modern-node-polyfills","lang":"bash","label":"npm"},{"cmd":"yarn add modern-node-polyfills","lang":"bash","label":"yarn"},{"cmd":"pnpm add modern-node-polyfills","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer dependency for internal bundling and parsing operations. It must be installed alongside modern-node-polyfills.","package":"esbuild","optional":false}],"imports":[{"note":"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.","wrong":"const { polyfillPath } = require('modern-node-polyfills')","symbol":"polyfillPath","correct":"import { polyfillPath } from 'modern-node-polyfills'"},{"note":"All public utilities are named exports; there is no default export. This function helps replace global expressions and module imports within code.","wrong":"import inject from 'modern-node-polyfills'","symbol":"inject","correct":"import { inject } from 'modern-node-polyfills'"},{"note":"This utility is crucial for automatically replacing common Node.js global expressions (like `process`, `Buffer`, `__filename`) with their polyfilled equivalents.","wrong":"const polyfillGlobals = require('modern-node-polyfills').polyfillGlobals","symbol":"polyfillGlobals","correct":"import { polyfillGlobals } from 'modern-node-polyfills'"}],"quickstart":{"code":"import { polyfillGlobals } from 'modern-node-polyfills';\n\nconst browserCode = `\n    console.log(global);\n    console.log(process.env.NODE_ENV);\n    const buf = Buffer.from('hello');\n    console.log(buf.toString());\n    console.log(__filename);\n    console.log(__dirname);\n`;\n\nasync function transformAndLog() {\n  try {\n    const polyfilledContent = await polyfillGlobals(\n      browserCode,\n      { \n        __filename: '/app/index.js',\n        __dirname: '/app' \n      }\n    );\n    console.log('--- Original Code ---\\n' + browserCode);\n    console.log('\\n--- Polyfilled Content ---\\n' + polyfilledContent);\n    // In a real browser/edge environment, you would now execute polyfilledContent\n  } catch (error) {\n    console.error('Error during polyfill:', error);\n  }\n}\n\ntransformAndLog();","lang":"typescript","description":"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."},"warnings":[{"fix":"Ensure `esbuild` is installed in your project: `npm install esbuild` or `yarn add esbuild`.","message":"This package requires `esbuild` as a peer dependency, but it's not automatically installed. Failure to install `esbuild` will lead to runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Install esbuild as a direct dependency: `npm install esbuild` or `yarn add esbuild`.","cause":"The `esbuild` peer dependency is missing from your project's `node_modules`.","error":"Error: Cannot find module 'esbuild'"},{"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.","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.","error":"ERR_MODULE_NOT_FOUND or Cannot read properties of undefined (reading 'polyfillPath') when using require()"},{"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.","cause":"Your code is attempting to access Node.js global variables that have not been polyfilled or injected into the environment.","error":"ReferenceError: process is not defined (or Buffer is not defined, global is not defined)"}],"ecosystem":"npm"}