msqar
msqar

Reputation: 3020

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.

Upvotes: 7

Views: 13730

Answers (2)

Ravi Sharma
Ravi Sharma

Reputation: 527

You can use react native library

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

console.log("App Version",appVersion);

Upvotes: 11

Chase
Chase

Reputation: 2304

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';

Upvotes: 8

Related Questions