esbuild-plugin-node-polyfills

raw JSON →
1.0.2 verified Fri May 01 auth: no javascript maintenance

Provides Node.js built-in module polyfills for esbuild bundler, similar to Webpack's 'node' option. Version 1.0.2 (latest, 2021) is stable but unmaintained since. Requires manual installation of peer dependencies: browserify-zlib, buffer, crypto-browserify, events, https-browserify, os-browserify, path-browserify, process, stream-browserify, stream-http, url, util. Compared to alternatives like @esbuild-plugins/node-globals-polyfills, this plugin offers a more complete set of polyfills but lacks ESM support and TypeScript definitions.

error Cannot find module 'buffer'
cause Missing peer dependency 'buffer' is not installed.
fix
npm install buffer
error Error: The plugin 'ESBuildNodePolyfillsPlugin' is not a function
cause Using import instead of require; the package exports a function via CommonJS.
fix
Change to: const ESBuildNodePolyfillsPlugin = require('esbuild-plugin-node-polyfills');
gotcha All dependencies must be installed manually as peer dependencies; automatic installation is not provided.
fix Run: npm install browserify-zlib buffer crypto-browserify events https-browserify os-browserify path-browserify process stream-browserify stream-http url util
gotcha The plugin only works with CommonJS; it cannot be imported using ES module syntax (import).
fix Use require() instead of import.
gotcha Polyfills are injected globally, which may cause unexpected side effects in libraries that expect the original Node.js behavior.
fix Consider using @esbuild-plugins/node-globals-polyfills for more controlled polyfilling.
npm install esbuild-plugin-node-polyfills
yarn add esbuild-plugin-node-polyfills
pnpm add esbuild-plugin-node-polyfills

Demonstrates how to configure esbuild with the node polyfills plugin using CommonJS.

const ESBuildNodePolyfillsPlugin = require('esbuild-plugin-node-polyfills');
const esbuild = require('esbuild');

require('esbuild').build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outfile: 'dist/bundle.js',
  plugins: [ESBuildNodePolyfillsPlugin],
}).catch(() => process.exit(1));