I would like to load a non standard variable from my package.json into my React frontend. For the standard package.json variables this is easily done by adding a reference to them in a .env file and then loading it in the frontend through process.env
:
.env:
REACT_APP_VERSION=$npm_package_version
REACT_APP_VERSION=$npm_package_name
React frontend:
console.log(process.env.REACT_APP_VERSION)
However, when I try this with a custom variable like so:
.env:
REACT_APP_MY_VARIABLE=$npm_package_myVariable
React frontend:
console.log(process.env.REACT_APP_MY_VARIABLE)
the variable is set to an empty string in process.env
I could import package.json directly but there are some security risks that come with that
Is there a safe way to import a custom variable into a React app from package.json or do I need to find another solution?