Accepted answer

Thanks to the help of @Bhojendra Rauniyar I got this solution:

  1. Add this line to public/index.html:

<meta build-version="%REACTBUILDVERSION%"/>

  1. Run this 3 commands:
REACTBUILDVERSION=$(date +%Y-%m-%d)-$(date +%T)
sed -i -- 's/%REACTBUILDVERSION%/'$REACTBUILDVERSION'/g' build/index.html
echo React Build Version = $REACTBUILDVERSION

To make it comfortable, these 3 commands can be added to the build in package.json:

Original build in package.json:

"build": "react-scripts build",

NEW build in package.json:

     "react-scripts build &&
      REACTBUILDVERSION=$(date +%Y-%m-%d)-$(date +%T) &&
      sed -i -- 's/%REACTBUILDVERSION%/'$REACTBUILDVERSION'/g' build/index.html &&
      echo React Build Version = $REACTBUILDVERSION",

After the build you can see this meta tag in the build/index.html:

<meta build-version="2019-10-16-16:31:43"/>


You can also add a version key pair to your package.json file "version": "1.0.0" and then you can read it anywhere in your code as follows let packageJson = require('./package.json') then doing packageJson.version should give you access to the version that is set in the package.json file a similar thing happens when the version is placed in app.json


I follow the following approach (will add git branch name in the version):

# First add '<!-- version: %VERSION% -->' to your public/index.html file
$ yarn run build # npm run build (if using npm)
$ VERSION=`git rev-parse --abbrev-ref HEAD`
$ sed -i -- "s/%VERSION%/$VERSION/g" build/index.html

Now your build/index.html should contain <!-- version: name-of-the-current-branch -->.


To make it comfortable, you may add version command in package.json:

"build": "VERSION=`git rev-parse --abbrev-ref HEAD` && 
         react-scripts build && 
         sed -i -- 's/%VERSION%/$VERSION/g' build/index.html"

Now, you can simply run:

yarn build # or, npm run build

Related Query

More Query from same tag