Angular Build Information CLI
raw JSON →angular-build-info is a Command Line Interface (CLI) tool designed to automate the collection of crucial build-related metadata for Angular applications. Upon execution, it generates a `build.ts` file, typically placed in the project's `src/` directory, containing details such as the application's version (from `package.json`), the current Git commit hash, the Git user, and a precise build timestamp. This generated file can then be directly imported and utilized within Angular components or services to display build information, useful for debugging, support, or informational purposes. The package is currently stable at version 2.0.1, having undergone a significant rewrite in version 2.0.0. Releases appear to follow an as-needed cadence, focusing on bug fixes and enhancements. Its primary differentiator is its simplicity and direct integration into existing Angular CLI build workflows via `package.json` scripts, providing an easily consumable TypeScript module.
Common errors
error Error: Failed to run git command: fatal: not a git repository (or any of the parent directories): .git ↓
git init in your project's root directory. error Module not found: Error: Can't resolve '../build' in '.../app.component.ts' ↓
angular-build-info --init has been run at least once to create the file, and that angular-build-info is executed before ng build in your package.json scripts (e.g., "build": "angular-build-info && ng build"). Verify src/build.ts exists. error Console logs show outdated build version or timestamp after a new deployment. ↓
package.json build scripts to ensure angular-build-info runs correctly before ng build. You might also need to clear build caches or force a clean build, e.g., by deleting dist/ and node_modules/.angular/cache/. Warnings
breaking Version 2.0.0 introduced a "Massive v2-Rewrite" which likely includes breaking changes in CLI arguments, configuration, or the structure/content of the generated `build.ts` file. Users upgrading from v1.x should consult the project's GitHub repository for specific migration instructions. ↓
gotcha `angular-build-info` relies on Git to fetch commit hash and user information. If Git is not installed on the build environment or the project directory is not a Git repository, the CLI may fail or produce incomplete build information (e.g., missing hash/user). ↓
gotcha For the `build.ts` file to contain the latest information, the `angular-build-info` command must be executed *before* `ng build` (or similar build commands) in your `package.json` scripts. Failure to do so will result in outdated or incorrect build metadata. ↓
Install
npm install angular-build-info yarn add angular-build-info pnpm add angular-build-info Imports
- buildInfo wrong
const buildInfo = require('../build');correctimport { buildInfo } from '../build';
Quickstart
npm i -g angular-build-info
# Initialize the build.ts file in your src/ folder
angular-build-info --init
# Add to package.json scripts
// package.json
{
"scripts": {
"build": "angular-build-info && ng build",
"deploy": "angular-build-info && ng build --prod && ./deploy"
}
}
// app.component.ts (example usage)
import { Component } from "@angular/core";
import { buildInfo } from "../build";
import { environment } from "../environments/environment";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
constructor() {
console.log(
`\n%cBuild Info:\n\n` +
`%c ❯ Environment: %c${environment.production ? "production 🏭" : "development 🚧"}\n` +
`%c ❯ Build Version: ${buildInfo.version}\n` +
` ❯ Build Timestamp: ${buildInfo.timestamp}\n` +
` ❯ Build Message: %c${buildInfo.message || "<no message>"}\n`,
"font-size: 14px; color: #7c7c7b;",
"font-size: 12px; color: #7c7c7b",
environment.production ? "font-size: 12px; color: #95c230;" : "font-size: 12px; color: #e26565;",
"font-size: 12px; color: #7c7c7b",
"font-size: 12px; color: #bdc6cf"
);
}
}