15

How can I use environment variables defined in .bash_profile in a React application? I have two React apps in production (they're the same project, so they have the same code), but they need to request to different API hosts, and I figured env variables could do the trick.

5
  • 2
    have you tried anything? Commented Jun 22, 2016 at 18:43
  • 1
    Do you use some kind of module bundler ? webpack ? browserify ? Commented Jun 22, 2016 at 19:47
  • You don't state what you are using, but usually this is done in the server side as I doubt JS can read any environment variables. In node you could do process.env.API_HOST Commented Jun 22, 2016 at 21:26
  • @topheman We're using webpack Commented Jun 22, 2016 at 22:29
  • @eblin, I don't mean for the JS running in the client to be able to read env variables. What we want is to insert values from those variables on compilation time. Commented Jun 22, 2016 at 22:29

2 Answers 2

23

Use webpack.DefinePlugin. Say you exported FOO and BAR in your .bash_profile, then your webpackconfig should look like:

const config = {
  entry: 'somescript',
  // ...
  module: {
    // ...
  },
  // ...
  plugins: [
    // ... your plugins
    new webpack.DefinePlugin({
      'process.env':{
        'FOO': process.env.FOO,
        'BAR': process.env.BAR
      }
    })
  ],
  // ...
}

You will be able to access those in your js at compile time via process.env.FOO & process.env.BAR

Resource: https://github.com/topheman/webpack-babel-starter

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

4 Comments

As you said it only will work at build time. @sauronnikko said that he has two projects with the same code. According to your solution, he will have two different applications i.e with different code.
just read a comment to the ticket and it seems it what they need. Sorry man.
@MikhailChibel Yes, sorry the question wasn't clear. I wanted the variables to work at build time
I also had to stringify the var like so: JSON.stringify(process.env.FOO) otherwise it will not work. Btw, I come from the future.
-2

Store the environment variables in the index.html!

if you treat the index.html like a deployment manifest that contains environment-specific values, you can use the same versioned assets (js, css) in every environment!

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="https://assets.myapp.com/favicon.ico">
    <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="manifest" href="https://assets.myapp.com/manifest.json">
    <title>React App</title>
    <script>
      env = {
        // Add your environment variables here
        api: "https://staging-api.myapp.com"
      };
    </script>
    <link href="https://assets.myapp.com/static/css/main.6bd13355.chunk.css" rel="stylesheet">
</head>
<body>
    <div id="root"></div>
    <script src="https://assets.myapp.com/static/js/1.a700ff87.chunk.js"></script>
    <script src="https://assets.myapp.com/static/js/main.3ec5cdc6.chunk.js"></script>
</body>
</html>

In your code reference the values directly: myApi = window.env.api;

Read more documentation for this methodology at https://immutablewebapps.org

1 Comment

I am looking for feedback on how I can improve this answer or if I should remove it -- thank you!

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.