rollup-plugin-git-info
raw JSON → 1.0.0 verified Mon Apr 27 auth: no javascript
A Rollup plugin that injects git build metadata (branch, commit hash, abbreviated hash, version, dates) into imported package.json files. Version 1.0.0 requires Node.js 12+ and Rollup 2+. This plugin is minimal and focused: it relies on @rollup/plugin-json for JSON handling and git CLI commands for data collection. Unlike alternatives that use environment variables or require manual configuration, it automatically populates standard fields like gitVersion and buildDate with no setup beyond adding the plugin.
Common errors
error Error: Cannot find module '@rollup/plugin-json' ↓
cause @rollup/plugin-json is a required peer dependency but not installed.
fix
Run: npm install @rollup/plugin-json --save-dev
error TypeError: gitInfo is not a function ↓
cause Incorrect import syntax, likely named import instead of default.
fix
Use: import gitInfo from 'rollup-plugin-git-info';
error Error: No git repository found in /path/to/project ↓
cause The plugin requires a .git directory in the project root.
fix
Initialize a git repository: git init
error SyntaxError: Unexpected token 'export' ↓
cause The plugin is ESM-only and cannot be used with CommonJS require directly.
fix
Switch to import syntax or use .default property: const gitInfo = require('rollup-plugin-git-info').default;
Warnings
breaking @rollup/plugin-json must be installed separately and declared before gitInfo in plugins array. ↓
fix npm install @rollup/plugin-json --save-dev and add json() before gitInfo().
gotcha The plugin only works if you import './package.json' in your source; importing from a different path will not include metadata. ↓
fix Ensure imports reference the correct package.json relative to the module.
gotcha git version is computed from the repository where package.json resides; using a monorepo with multiple package.json files may produce unexpected results. ↓
fix Set cwd option to the correct directory for each package.
gotcha The gitBranch field will be 'HEAD' in detached head state, which may break tools expecting a proper branch name. ↓
fix Handle 'HEAD' in application logic or use a fallback branch name.
deprecated The dateFormat option uses git log --date format; custom date formats may break with git version mismatches. ↓
fix Test date output across target git versions; consider using dateCommand for full control.
Install
npm install rollup-plugin-git-info yarn add rollup-plugin-git-info pnpm add rollup-plugin-git-info Imports
- gitInfo wrong
const gitInfo = require('rollup-plugin-git-info');correctimport gitInfo from 'rollup-plugin-git-info'; - gitInfo wrong
const gitInfo = require('rollup-plugin-git-info');correctconst gitInfo = require('rollup-plugin-git-info').default; - gitInfo wrong
import { gitInfo } from 'rollup-plugin-git-info';correctimport gitInfo from 'rollup-plugin-git-info';
Quickstart
import gitInfo from 'rollup-plugin-git-info';
import json from '@rollup/plugin-json';
export default {
input: 'src/index.js',
output: {
dir: 'dist',
format: 'es',
},
plugins: [
json(),
gitInfo(),
],
};