Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to get my app version in react-native

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.

like image 993
msqar Avatar asked Jan 16 '19 19:01

msqar


People also ask

How do I find the react-native version of a project?

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).

How do I update my version in react-native?

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 ... ... ... } }


2 Answers

You can use react native library

import DeviceInfo from 'react-native-device-info';
const appVersion = DeviceInfo.getVersion();

console.log("App Version",appVersion);
like image 158
Ravi Sharma Avatar answered Sep 30 '22 08:09

Ravi Sharma


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';
like image 34
Chase Avatar answered Sep 30 '22 08:09

Chase