{"id":21955,"library":"rollup-plugin-codegen","title":"rollup-plugin-codegen","description":"A Rollup plugin for generating source code modules at bundle time. Current stable version is 2.1.0, released with fixes and support for Bun and Parcel loaders. This plugin allows dynamic generation of modules based on configuration, useful for injecting environment variables, version info, or computed code. Key differentiator: it integrates directly into Rollup's build process, avoiding separate pre-build steps. Ships TypeScript types, supports ESM and CJS outputs, requires Node >=16.0. Alternative to virtual modules or manual file writes.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/FlorianRappl/codegen-js","tags":["javascript","rollup","codegen","plugin","typescript"],"install":[{"cmd":"npm install rollup-plugin-codegen","lang":"bash","label":"npm"},{"cmd":"yarn add rollup-plugin-codegen","lang":"bash","label":"yarn"},{"cmd":"pnpm add rollup-plugin-codegen","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; requires Rollup version 2 or higher.","package":"rollup","optional":false}],"imports":[{"note":"ESM-only since v2; named export, not default. TypeScript types included.","wrong":"import codegen from 'rollup-plugin-codegen'","symbol":"codegen","correct":"import { codegen } from 'rollup-plugin-codegen'"},{"note":"Type export for TypeScript users; not available in CJS.","wrong":"const { Plugin } = require('rollup-plugin-codegen')","symbol":"Plugin","correct":"import type { Plugin } from 'rollup-plugin-codegen'"},{"note":"Use this type for the options object parameter.","wrong":"","symbol":"CodegenOptions","correct":"import type { CodegenOptions } from 'rollup-plugin-codegen'"}],"quickstart":{"code":"import { codegen } from 'rollup-plugin-codegen';\nimport { defineConfig } from 'rollup';\n\nexport default defineConfig({\n  input: 'src/index.js',\n  plugins: [\n    codegen({\n      modules: [\n        {\n          id: 'src/env',\n          code: `export const NODE_ENV = ${JSON.stringify(process.env.NODE_ENV ?? 'development')};`\n        }\n      ]\n    })\n  ],\n  output: { dir: 'dist', format: 'esm' }\n});","lang":"typescript","description":"Shows how to configure codegen plugin to generate a module with environment variable."},"warnings":[{"fix":"Use `import { codegen } from 'rollup-plugin-codegen'` instead of `import codegen from 'rollup-plugin-codegen'`","message":"Version 2.0.0 changed from default export to named export","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade Node.js to version 16 or later.","message":"Node version requirement changed to >=16.0","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure each module id is a unique relative path string (e.g., 'src/generated/env').","message":"Generated module IDs must be relative and unique; duplicate IDs cause errors","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Change `import codegen from 'rollup-plugin-codegen'` to `import { codegen } from 'rollup-plugin-codegen'`","cause":"Using default import instead of named import in ESM environment.","error":"Error: The plugin 'codegen' does not export a default export."},{"fix":"Run `npm install rollup-plugin-codegen` or `yarn add rollup-plugin-codegen`.","cause":"Package not installed or not in node_modules.","error":"Error: Cannot find module 'rollup-plugin-codegen'"},{"fix":"Ensure the module id is a relative path that does not conflict with existing files. Use a path like 'src/generated/env' that doesn't exist on disk.","cause":"The module ID path does not match Rollup's resolution or file system.","error":"Error: The generated module 'src/env' could not be resolved."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}