Get version number from package.json in React Redux

OP EDIT: if anyone else comes across this: the application was created using create-react-app , which restricts imports inside the src folder. However, if you upgrade reactive scripts to version 1.0.11 , this will allow you to access package.json.

I am trying to get the version number from package.json in my application.

I already tried these suggestions , but none of them worked, because I cannot access package.json because of the src folder (maybe because of the React, I'm new to this). Moving package.json to src then means that I cannot run npm install, npm version minorand npm run buildfrom my root folder. I tried using process.env.npm_package_version, but this leads to undefined.

I use Jenkins and I haven't configured it yet to push commits, but the only idea I have is to get the version from tags in GitLab, but I have no idea how to do this, and this will add an unnecessary repo dependency, therefore, I would really like to find an alternative.

EDIT: My file structure is similar:

--> RootAppFolder
    |--> build
    |--> node_modules
    |--> public
    |--> src
         |--> Components
              |--> Root.js
    |
    |--> package.json

, package.json Root.js, import packageJson from './../../package.json', :

./src/components/Root.js

: . /../../package.json, src/ . src/ . src/, node_modules/.

+6
2

import packageJson from '/package.json';

:

# From the project root.
cd src; ln -s ../package.json package.alias.json

src, .

ls
#=> package.alias.json -> ../package.json

.alias , . , . . , JS- ./package.alias.json ./package.json.

: create-react-app src

+3

.

// in package.json
"version": "1.0.0"

// in index.js
import packageJson from '../package.json';
console.log(packageJson.version); // "1.0.0"
+6

All Articles