6

So I'm trying to understand how to use .env to secure my api key & data for my login auth. I've followed the process of the dotenv package and the stackoverflow answer here.

What I did:

1. I installed the dotenv package

2. In my rootfolder, I added a .env with only one line:

API_AUTH=http://myapilink.com

3. In my App.js, I added following line:

require('dotenv').config()
console.log(process.env.API_AUTH)

However, console.log returns 'undefined', why is that? What have I missed?

2
  • Did you generate your project using create-react-app? Commented Jan 13, 2019 at 12:49
  • I believe I did. I'm currently testing this on a react app I've been working on for a month now. But at the time, I followed Reacts guideline to create my react app Commented Jan 13, 2019 at 12:53

2 Answers 2

18

You have to prefix your environment variables with REACT_APP_. See here.

Note: You must create custom environment variables beginning with REACT_APP_. Any other variables except NODE_ENV will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.

So instead of just API_AUTH it would be REACT_APP_API_AUTH instead.

And it will be exposed to your app as process.env.REACT_APP_API_AUTH.

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

8 Comments

After changing the variable name with the REACT_APP_ prefix, I still get undefined. However, I now console.log(process.env) to see what that returns and get the following: Object NODE_ENV: "development" PUBLIC_URL: "" __proto__: Object I don't know if that helps.
@CédricBloem Did you restart your server after changing it? If that doesn't work, try changing your .env to .env.development and restart it again.
I did and it worked. Can't believe the "have you tried turning it on and off again" did it :) Thanks!!
@CédricBloem You don't need dotenv when working with CRA. It is bundled there anyways.
Yup, tried and tested solution for every computer problems.:). And you're welcome.
|
4

Dotenv only works on server side. It needs an environment to store the variables. Here, we extract the content of the .env file and reduce it to an Object and pass it to Webpack's DefinePlugin allowing us later usage (process.env.API_AUTH):

const webpack = require('webpack');
const dotenv = require('dotenv');

module.exports = () => {
  const env = dotenv.config().parsed,
    envKeys = Object.keys(env).reduce((prev, next) => {
      prev[`process.env.${next}`] = JSON.stringify(env[next]);
      return prev;
    }, {});

  return {
    plugins: [
      new webpack.DefinePlugin(envKeys)
    ]
  };

3 Comments

Thanks for your answer! But apparently the mistake I made was forgetting to use the 'REACT_APP_' prefix for my variables.
In that one, you do not need the prefix.
Env variables were easy in #React App done with CRA. But, I faced the issue doing with #webpack and it was resolved with your solution. However, there is the #EnvironmentPlugin, which is equivalent to #DefinePlugin but with lesser code. Reference

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.