Modern Node Polyfills

1.0.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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();

view raw JSON →