Toine Seiter
Toine Seiter

Reputation: 447

yarn build + create-react-app + env variables in public JS file

I'm using yarn and env-cmd to build my React app and manage our environment variables. Here is my package.json :

"scripts": {
    "start": "react-scripts start",
    "build:local": "env-cmd .env.local react-scripts build",
    "build:uat": "env-cmd .env.uat react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },

It works fine for the JS files under the /src/ folder and in /public/index.htm too.

Question: How can I use a variable in a public/*.js file ?

My variable is named REACT_APP_URL. I've tried to use %REACT_APP_URL% or process.env.REACT_APP_URL without success.

EDIT: I want to inject these variables at build time.

Upvotes: 2

Views: 6401

Answers (1)

user7143626
user7143626

Reputation:

I have a file .env.development in the same src with package.json this works for me

"start": "REACT_APP_ENV=develpoment npm-run-all -p watch-css start-js",

Upvotes: 2

Related Questions