esbuild plugin that defines global identifiers at build time, similar to esbuild's built-in `define` feature but with a more structured and discoverable API. Version 0.6.0 is the latest stable release as of early 2025, with active development and monthly updates. Unlike manual `define` objects, this plugin supports nested object definitions (e.g., `process.env.API_KEY`) and merges with esbuild's native `define`. It ships TypeScript types and is designed for modern Node.js (>=20) with native ESM support. Key differentiator: avoids tedious string escaping for complex replacement values and provides a cleaner configuration pattern.
npm install esbuild-plugin-defineVerified import paths — ran on the pinned version, not inferred.
Shows how to configure the plugin with nested object definitions and a string literal, then run esbuild programmatically.
Use import { definePlugin } from 'esbuild-plugin-define' or destructure require: const { definePlugin } = require('esbuild-plugin-define').Upgrade Node.js to 20 or later.
Use plain nested objects: { process: { env: { KEY: 'value' } } } instead of { process: { env: { KEY: { value: 'value', scope: '...' } } } }.For complex replacements, ensure the value is a valid JS expression. Use .toString() or JSON.stringify manually if needed.
List definePlugin first in the plugins array.
Use const { definePlugin } = require('esbuild-plugin-define');Ensure the define object keys are strings starting with a letter, not numbers or symbols.
Move the define object to definePlugin() or convert it to string literals for esbuild's define.
Either rename to .cjs or use dynamic import: const { definePlugin } = await import('esbuild-plugin-define');