esbuild plugin that replaces process.env references with environment variable values at build time. Supports both object maps (key → default value) and arrays of keys. Version 0.5.0 is the latest stable release, with active maintenance. Key differentiators: simple API, supports both Node.js and browser targets, ships TypeScript definitions, and integrates seamlessly with esbuild's plugin system. The plugin is lightweight and focuses exclusively on environment variable injection, unlike more complex solutions like dotenv or webpack's DefinePlugin.
npm install esbuild-plugin-environmentVerified import paths — ran on the pinned version, not inferred.
Shows ESM usage with object map: defines API_URL (with fallback) and NODE_ENV as build-time constants.
Upgrade esbuild to version 0.15 or higher
Use dynamic import or switch project to ESM
Upgrade Node.js to 20 or later
Pass object map with fallbacks or ensure environment variables are set before build
Use esbuild's external or exclude plugin for specific dependencies
Ensure environmentPlugin is the first plugin in the plugins array
Add inject plugin with process polyfill instead of environmentPlugin for browser targets