1

I used this solution but it didn't work for me.

My React app(18.2.0v) was initialized with CRA(5.0.1v). My dotenv version is 16.0.3.

On development version I get package.json file version successfully, but on build version I can't get it.

My .env.staging file:

NODE_ENV=development
REACT_APP_VERSION=$npm_package_version

My package.json scripts:

{
  ...
  "version": "0.1.0",
  ...      
  "scripts":
    {
      ...
      "start": "react-scripts start",
      ...
      "build:staging": "npm run clean && env-cmd -f .env.staging react-scripts build",
      ...
    }
...
}

My Login.tsx display version this way:

...
const { REACT_APP_VERSION } = process.env;
...
return (
...
<h1>V{REACT_APP_VERSION}</h1>
...
)
...

Dev mode display print

Build mode display print

I have an .env.development with the same content as the current .env.staging quoted above that will be used for another build version.

Have someone any idea how to solve this issue, please?

1 Answers1

0

I solved this when I installed dotenv-cli and changed package.json build:staging script to "npm run clean && dotenv -e .env.staging react-scripts build"