React Build Info Generator
react-build-info is a development utility designed for React projects to automatically inject build-time metadata into the application. It reads the project's `package.json` file to extract the `version` and combines it with the current date/time to generate a `src/buildInfo.ts` (or `.js`) file. This file contains an exported constant `buildInfo` with `buildVersion` and `buildDate` properties, making build information easily accessible within the React application for debugging, support, or display purposes. The current stable version is `0.0.3`. This package is a low-churn utility, receiving updates primarily as needed to maintain compatibility or address specific issues. Its key differentiator is its simplicity and direct file generation approach, avoiding the overhead of more complex build tool plugins.
Common errors
-
Module not found: Can't resolve './buildInfo' in '...' or similar compilation error related to missing 'buildInfo' file.
cause The `react-build-info` command was not executed, or it failed, leading to the `src/buildInfo.ts` file not being generated before the application build process started.fixEnsure `react-build-info` is correctly integrated into your `package.json` build script (e.g., `"build": "react-build-info && react-scripts build"`) and runs successfully. -
TypeError: Cannot read properties of undefined (reading 'buildVersion') or 'buildInfo' is undefined.
cause This typically occurs when `buildInfo` is imported incorrectly as a default export, or the generated file is empty/malformed.fixVerify that `src/buildInfo.ts` was generated correctly. Most importantly, ensure you are using a named import: `import { buildInfo } from './buildInfo';`
Warnings
- gotcha Manual Integration Required: The `react-build-info` tool does not automatically run. You must manually add it to your project's `package.json` build script or another appropriate lifecycle hook to ensure `src/buildInfo.ts` is generated/updated before your main application build.
- gotcha Version Increment Dependency: To ensure `buildVersion` reflects a new version, the `npm version patch` (or `minor`, `major`) command must be executed *before* `react-build-info` in your build script. Without it, `react-build-info` will use the existing `package.json` version.
- breaking Incorrect Import Syntax: The generated `src/buildInfo.ts` file exports `buildInfo` as a named constant (`export const buildInfo = { ... }`). Attempting to import it as a default export (`import buildInfo from './buildInfo';`) will result in `buildInfo` being `undefined` or a module resolution error.
- gotcha File Overwriting: The `react-build-info` command overwrites the `src/buildInfo.ts` (or `.js`) file every time it runs. Do not manually edit this file as your changes will be lost on the next build.
Install
-
npm install react-build-info -
yarn add react-build-info -
pnpm add react-build-info
Imports
- buildInfo
import buildInfo from './buildInfo';
import { buildInfo } from './buildInfo'; - React
import React from 'react';
- BuildInfoType
type BuildInfoType = { buildVersion: string; buildDate: number; };
Quickstart
import React from 'react';
import { buildInfo } from './buildInfo'; // Correct import for named export
import './App.css'; // Assuming standard React app structure
const buildDate = new Date(buildInfo.buildDate);
class App extends React.Component {
componentDidMount() {
console.log(`Application Build Number: ${buildInfo.buildVersion}`);
console.log(`Application Build Date: ${buildDate.toLocaleString()}`);
// You could also send this info to an analytics service or display it in a footer
}
render() {
return (
<div className="App">
<header className="App-header">
<h1>My Application</h1>
<p>Version: {buildInfo.buildVersion}</p>
<p>Built On: {buildDate.toLocaleDateString()} {buildDate.toLocaleTimeString()}</p>
</header>
<main>
<p>Welcome to the app!</p>
</main>
<footer>
<p>Powered by React</p>
</footer>
</div>
);
}
}
export default App;