{"id":25266,"library":"esbuild-codegen-plugin","title":"esbuild-codegen-plugin","description":"An esbuild plugin (v2.1.0) for generating modules at bundle time, allowing dynamic code injection during builds. Part of the codegen-js monorepo, it provides a core library (`codegen-lib`) and loaders for esbuild, webpack, parcel, and bun. Supports both ESM and CJS output. Key differentiator: code generation at bundle time rather than pre-build scripts, enabling type-safe dynamic imports and real-time code generation based on build context. Released monthly with active maintenance.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/FlorianRappl/codegen-js","tags":["javascript","plugin","esbuild","build","codegen","typescript"],"install":[{"cmd":"npm install esbuild-codegen-plugin","lang":"bash","label":"npm"},{"cmd":"yarn add esbuild-codegen-plugin","lang":"bash","label":"yarn"},{"cmd":"pnpm add esbuild-codegen-plugin","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency: plugin runs on esbuild's plugin system","package":"esbuild","optional":false}],"imports":[{"note":"ESM-only since v2.0.0; CommonJS require will fail.","wrong":"const esbuildCodegenPlugin = require('esbuild-codegen-plugin')","symbol":"esbuildCodegenPlugin","correct":"import { esbuildCodegenPlugin } from 'esbuild-codegen-plugin'"},{"note":"Default export is same as named export; both work but named is idiomatic.","wrong":"import { default as esbuildCodegenPlugin } from 'esbuild-codegen-plugin'","symbol":"default","correct":"import esbuildCodegenPlugin from 'esbuild-codegen-plugin'"},{"note":"PluginConfig is a TypeScript type; runtime import will be undefined.","wrong":"import { PluginConfig } from 'esbuild-codegen-plugin'","symbol":"PluginConfig","correct":"import type { PluginConfig } from 'esbuild-codegen-plugin'"}],"quickstart":{"code":"import { build } from 'esbuild';\nimport { esbuildCodegenPlugin } from 'esbuild-codegen-plugin';\nimport { fileURLToPath } from 'url';\n\nconst __dirname = fileURLToPath(new URL('.', import.meta.url));\n\nawait build({\n  entryPoints: ['./src/app.ts'],\n  bundle: true,\n  outfile: './dist/bundle.js',\n  plugins: [\n    esbuildCodegenPlugin({\n      // Generate a module that exports current build time\n      './build-time.ts': `export const buildTime = ${JSON.stringify(Date.now())};`,\n    }),\n  ],\n});","lang":"typescript","description":"Shows how to use esbuild-codegen-plugin to inject a generated module (build-time constant) during esbuild bundling."},"warnings":[{"fix":"Switch to ESM imports: import { esbuildCodegenPlugin } from 'esbuild-codegen-plugin'.","message":"v2.0.0 dropped CommonJS support — require() now throws.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use paths like './my-module.ts' instead of '/absolute/path/my-module.ts'.","message":"Generated module paths must be relative to project root, not absolute.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use unique paths or namespaced prefixes for generated modules.","message":"Plugin runs during build but before resolve — ensure generated modules don't conflict with existing files.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Replace generate(config)(esbuildConfig) with esbuildCodegenPlugin(config) in plugin array.","message":"The 'generate' function from v1.x is removed; use the plugin constructor directly.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run npm install esbuild-codegen-plugin --save-dev","cause":"Package not installed or resolution failure.","error":"Error: Cannot find module 'esbuild-codegen-plugin'"},{"fix":"Use import { esbuildCodegenPlugin } from 'esbuild-codegen-plugin' (ESM only).","cause":"Using default import destructured incorrectly or using require with v2+.","error":"TypeError: esbuildCodegenPlugin is not a function"},{"fix":"Call the function: esbuildCodegenPlugin({...}) and pass its return value to plugins array.","cause":"Passing the plugin function incorrectly (e.g., without calling it).","error":"Error: Plugin must be an object with name and setup, got function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}