I got a react-native app that I created by using react-native init
and I'm trying to have like a global constant with my APP_VERSION so I can use it inside the Authentication/Login screen.
The only place I saw this variable is inside my package.json file. After searching for this, seems that other people is trying to get it from here. Although I wasn't that successful on doing it.
So the idea was to have a file like src/_global/utils.js
With this:
import * as pkg from '.package.json';
export const APP_VERSION = pkg.version;
But is throwing exceptions that module can't be resolved. Tried using require('.package.json'), didn't work either.
This is what the other method to get it looked like:
var pkg = require('.package.json');
Any way to get this version? Is there any other cleaner way to do it?
Thanks in advance.
If you type react-native --version inside a project, it will tell you the installed version (tested with react-native-cli v. 2.0. 1).
If you're using expo and getting this issue, go to app. json and change the version to a higher number. { "expo": { "name": "nameofapp", // btw dont copy this "slug": "appslug", // btw dont copy this "version": "1.0. 0", // here is where you change the version ... ... ... } }
You can use react native library
import DeviceInfo from 'react-native-device-info';
const appVersion = DeviceInfo.getVersion();
console.log("App Version",appVersion);
You're not importing your package.json correctly. You can also destructure it within the import.
import {name as app_name, version as app_version} from './path/to/package.json';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With