React Build Info Generator

0.0.3 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `react-build-info` into your React project's `package.json` build script and then consume the generated build information within a React component. This setup ensures that your application always displays the correct version and build timestamp, which is invaluable for debugging and support. Remember to add `"build": "npm version patch && react-build-info && react-scripts build"` to your `package.json` scripts section.

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;

view raw JSON →