Browserslist to esbuild target converter

raw JSON →
2.1.1 verified Mon Apr 27 auth: no javascript

Converts browserslist queries into esbuild-compatible target arrays (e.g., 'chrome79', 'firefox91'). Version 2.1.1 is current stable, pure ESM since v2.0.0, requiring Node >=18 and browserslist as a peer dependency. Unlike other approaches (manual mapping or build tool plugins), this package supports all browserslist config sources (package.json, .browserslistrc, inline strings) and handles edge cases like 'safari TP', IE, and Opera. TypeScript types included. Recommended for projects using esbuild with browserslist.

error Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/browserslist-to-esbuild/index.js not supported.
cause Package is pure ESM since v2; CommonJS require() fails.
fix
Use import('browserslist-to-esbuild') or migrate your project to ESM.
error TypeError: browserslistToEsbuild is not a function
cause Default import misused in CommonJS context or wrong import style.
fix
Import correctly: import browserslistToEsbuild from 'browserslist-to-esbuild' (ESM) or const { default: browserslistToEsbuild } = await import('browserslist-to-esbuild') (dynamic import).
error Cannot find module 'browserslist'
cause browserslist is a peer dependency not installed.
fix
npm install --save-dev browserslist
breaking Package is pure ESM since v2.0.0; CommonJS require() throws Error.
fix Use ESM imports (import) or dynamic import() in CommonJS projects.
breaking Node.js 18+ required since v2.0.0.
fix Upgrade Node.js to >=18 or use v1.x (v1.2.0 is last CommonJS-compatible).
deprecated browserslist became a peer dependency in v2.0.0. Missing it will cause runtime errors.
fix Install browserslist as a devDependency: npm install --save-dev browserslist
gotcha Calling browserslistToEsbuild() without arguments may throw if no browserslist config found in current directory.
fix Always provide a browserslistConfig argument or ensure a .browserslistrc or package.json browserslist field exists.
gotcha Safari Technology Preview (safari TP) is not supported and will cause esbuild to break; v2.0.0 fixed by ignoring it.
fix Upgrade to v2.0.0+ or filter out 'safari TP' manually.
gotcha IE and Opera targets are supported since v2.0.0; earlier versions may produce invalid esbuild targets for those browsers.
fix Upgrade to v2.0.0+ or handle IE/Opera manually.
npm install browserslist-to-esbuild
yarn add browserslist-to-esbuild
pnpm add browserslist-to-esbuild

Demonstrates converting a browserslist query (default config) into esbuild target array.

import { build } from 'esbuild';
import browserslistToEsbuild from 'browserslist-to-esbuild';

await build({
  entryPoints: ['src/index.js'],
  outfile: 'dist/output.js',
  bundle: true,
  target: browserslistToEsbuild(['> 0.2%', 'not dead']),
});