1

I have an Ionic React Project created with Ionic Cli ionic start using the blank template.

How can I set the project's version that I see in the package.json file?

Is that sufficient? Is there any place that I should write the version as well? E.g.: tsconfig.json, ionic.config.json?

Can npm run build do that, like having some sort of version argument? Or do I need to make a script that parses those JSON files?

Vivere
  • 1,919
  • 4
  • 16
  • 35

2 Answers2

1

If you're using this project solely for web, then the package.json version is enough. And if you want to display it inside your app, you can do it like this (bottom left corner):

Version showing inside app

you can do the following:

import packageJson from "../../../package.json";

packageJson.version

If you're planning to change the version of any platform (android - ios - etc...) you will have to do that manually for each incremental version inside a configuration file specific for each platform (ex: for Android it's build.gradle file)

Ibrahim Awad
  • 498
  • 1
  • 6
  • 13
1

I ran into the same question. But after a bunch of research, parsing the whole package.json was not an option. I tried different approaches. Firstly reading appInfo from @capacitor/app, which won't work for the web builds. Then trying the variant with an .env file (REACT_APP_VERSION=$npm_package_version), which also doesn't work, because the $npm_package_% variables aren't available for ionic.

So I finally ended up with using genversion.

  1. npm install genversion --save-dev
  2. Patch your scripts section in package.json (see ionic:build:before hook):
{
  [...]
  "scripts": {
    [...]
    "ionic:build:before": "genversion --semi --es6 src/version.js"
  },
  [...]
}
  1. ionic build --prod
  2. Import version:
[...]
import { version } from '../version';
[...]
csaar
  • 530
  • 5
  • 22