2

Just another React Question. I want to store some URL information globally and divided by environment (dev, production etc...).

Searching on web, the usual method is .env files that are naturally supported by react.

I have create a .env and a .env.development so the first will be the production and the second the development file.

Inside the file, I put my configuration like:

# .env.development
REACT_APP_TEST=newyork

I tried to print the values with:

console.log(process.env);
console.log(process.env.REACT_APP_TEST);   

What I get is:

Object
  NODE_ENV:"development"
  PUBLIC_URL:""

The second is undefined.

Where is the problem?

2
  • This seems more like a node question than a react question Commented Jan 29, 2018 at 16:26
  • 1
    What fixed it for me was simply closing the terminal running my local dev server and re-running npm run start. Commented Oct 26, 2018 at 4:12

3 Answers 3

5

If you are using create-react-app, you will need to update your env.js to include your new environment variable in the build.

The function getClientEnvironment(publicUrl) sets the environment variables that get injected into the build for your use. Here, you can add your custom env variable like so:

  {
    // Useful for determining whether we’re running in production mode.
    // Most importantly, it switches React into the correct mode.
    NODE_ENV: process.env.NODE_ENV || 'development',
    // Useful for resolving the correct path to static assets in `public`.
    // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
    // This should only be used as an escape hatch. Normally you would put
    // images into the `src` and `import` them in code to get their paths.
    PUBLIC_URL: publicUrl,
    CUSTOM: process.env.CUSTOM || 'fallback'
  }

UPDATE:

If you're not using CRA, you can use the webpack DefinePlugin to do the same thing.

UPDATE 2:

@DimitarChristoff pointed out that you should use the REACT_APP_* format for declaring env variables. If you prepend your env variable with REACT_APP_, CRA will automatically pick it up and add it to the Webpack DefinePlugin:

// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
// injected into the application via DefinePlugin in Webpack configuration.
const REACT_APP = /^REACT_APP_/i;
Sign up to request clarification or add additional context in comments.

4 Comments

are you sure? cross-env REACT_APP_TEST=FOO npm start would make it available automagically. also, env.js has this: // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be // injected into the application via DefinePlugin in Webpack configuration. const REACT_APP = /^REACT_APP_/i; ...
@DimitarChristoff Nice, didn't know about that.
it probably is ignoring the file altogether. github.com/facebook/create-react-app/blob/master/packages/… - he should try .env.local first and if that works, he needs to export NODE_ENV correctly to development to load .env.development i guess
I've tried .env.local but it is still undefined. Actually im developing using npm start. Im not compiling the app with a webpack script. And in don't have any env.js in my project folder.
3

It was more simple than expected. The .env files has to be placed in the root of the project NOT in the SRC folder.

That's all!

Comments

1

In general Webpack case, you can use webpack's DefinePlugin to define variables to be accessible across app.

In CRA (Create-React-App), you can add the environment variable in env.js file.

Using env.js is same as making your own config.js file where you create your config variable with a value either from process.env or use a default.

Comments

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.