How to do app versioning in create react app?
Asked Answered
D

5

13

I need to display version of my react app in the footer in x.y.z format.

I need this version to increment every time I deploy the app by being provided a choice if I want to increment x or y or z.

How do I achieve this? :)

Dhow answered 30/1, 2020 at 2:51 Comment(0)
W
12

To bump the version of your app you can use npm version.

For example:

npm version minor
Whoa answered 29/10, 2020 at 10:9 Comment(1)
that's part of the solution but doesn't explain how to get it displayedFulgurous
K
13

Once you have a way to bump the version in package.json (e.g. npm version, as suggested by @bertrand-p), you can then assign the version to an environment variable. For example, in .env you can set:

REACT_APP_VERSION=$npm_package_version

Then you can access the variable from within your app via process.env.REACT_APP_VERSION.

See also: https://github.com/facebook/create-react-app/issues/2466#issuecomment-357490359

Katerinekates answered 3/9, 2021 at 16:24 Comment(4)
Thanks, this is working with yarn too. Is it okay to use REACT_APP_VERSION=$npm_package_version with yarn?Gavra
@SankaSanjeewa Yes, that should workKaterinekates
Do you know if I could link that to git's branch hash (git rev-parse HEAD)?Diandre
@Diandre I would assume you could override it with an arbitrary value from the shell e.g. REACT_APP_VERSION=$(git rev-parse HEAD) npm run buildKaterinekates
W
12

To bump the version of your app you can use npm version.

For example:

npm version minor
Whoa answered 29/10, 2020 at 10:9 Comment(1)
that's part of the solution but doesn't explain how to get it displayedFulgurous
F
2

I don't think the answers from @Bertrand P or @VulfCompressor tell the complete picture. I used genversion https://www.npmjs.com/package/genversion. The steps I did were:

  1. npm install genversion --save-dev
  2. Modify the build script in package.json to genversion --es6 src/autobuild_version.js && react-scripts build (I couldn't figure out a way to import the generated module from the lib directory as suggested in the genversion documentation so I had to put it in the src directory instead)
  3. In the React app, import { version } from './autobuild_version' and use as appropriate
  4. Add src/autobuild_version.js to .gitignore (other source code control tools are available)
  5. npm run build
Fulgurous answered 10/4, 2021 at 18:19 Comment(3)
have to give version whole app, then should I add it in index.js file or anything elseAhimsa
genversion creates a separate file containing just the version number. you can import it wherever you likeFulgurous
we can import from .env file also why should I use this package :-|Ahimsa
N
1

You can use npm commands below in different conditions mentioned below and the commands will effect the version number in package.json in your react project. You can access that version by installing dotenv by npm i dotenv oryarn add dotenv and use the .env file in your root folder and access the version number in the whole app with REACT_APP_VERSION=$npm_package_version. You can find more commands at npm version documentation page. commands below will add to version numbers like this: "version":"{major}.{minor}.{patch}"

If its a major change(bug fixing) in your app use:

npm version major

If its a minor change(bug fixing) in your app use:

npm version minor 

And if it's just a patch upgrade such as changing some styles use:

npm version patch

You can use versioning for many purposes and one of them is for making sure client's cache will be renew and they would have the updated content.

Nodarse answered 4/2, 2023 at 22:10 Comment(0)
F
0

You can use grunt-bump to handle your app versioning. As for displaying your app version, refer to Is there a way to get version from package.json in nodejs code?.

Flatting answered 30/1, 2020 at 6:3 Comment(1)
create react app isn't nodejs code - it's client-side javascript code and the accepted answer to that question is dangerous to do in the client.Fulgurous

© 2022 - 2024 — McMap. All rights reserved.