5

I have .env.local with

REACT_APP_BACKEND_BASEURL=http://localhost:8080/

and .env.development with

REACT_APP_BACKEND_BASEURL=http://deployedserverurl:8080/

how do I select the correct env file on start? As it is now, it prefers the dev env file over the local.

npm start --env=local doesnt seem to work, am I missing something?

1 Answer 1

7

Environment variables are imported depending on the current environment. There is a built in special environment variable with create-react-app called NODE_ENV. In summary, when you run npm start the NODE_ENV variable is set to development and set to production when running a npm run build. Therefore, if you create a .env.development in the root of your project, when you run npm start these variable definitions will be searched for in the environment.

Also, ensure you're using them correctly by using process.env.REACT_APP_APP_BACKEND_BASEURL.

If you need more information regarding all of the details about the different type of .env files, check these out from the React Docs:

env: Default.

.env.local: Local overrides. This file is loaded for all environments except test.

.env.development, .env.test, .env.production: Environment-specific settings.

.env.development.local, .env.test.local, .env.production.local: Local overrides of environment-specific settings.

Sign up to request clarification or add additional context in comments.

3 Comments

but what about accessing dev url without having to build it? I mean different env variables depending on my needs, will I have to need to manually edit them on the fly?
You can actually perform a check on NODE_ENV in your code if you need to only use certain variables during production or dev to prevent manually editing your variables.
The correct link for adding customer values is here create-react-app.dev/docs/adding-custom-environment-variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.