title: node programmatically set process environment variables not available to imported modules
body: <p>I'm new, hopefully this question is properly formatted and formulated. Can't wait to see your answers on this question. Let's get to it..</p>
  
  <h1>Context</h1>
  
  <p>Past weekend I was trying to implement <code>es2015</code> syntax support in my <code>create-react-app</code> configuration files, which was straight forward. All I had to do was use <code>babel-register</code> and <code>babel-preset-env</code> to get it working. So far so good you could say, however it wasn't all good. After some hours of searching I found that <code>process.env</code> variables are not passed through to imported modules. The code below will demonstrate my issue.</p>
  
  <h1>The code</h1>
  
  <p><strong>package.json</strong>  </p>
  
  <pre><code>{
    ...
    "scripts": [
      "good": "NODE_ENV=development BABEL_ENV=development node -r babel-register scripts/start.js",
      "bad": "node -r babel-register scripts/start.js"
    ],
    "devDependencies": {
      "babel-core": "^6.26.3",
      "babel-preset-env": "^1.7.0",
      "babel-register": "^6.26.0"
    }
    ...
  }
  </code></pre>
  
  <p><strong>.babelrc</strong>  </p>
  
  <pre><code>{
    "presets": [ "env" ]
  }
  </code></pre>
  
  <p><strong>scripts/start.js</strong>  </p>
  
  <pre><code>'use strict'
  
  process.env.NODE_ENV = 'development';
  process.env.BABEL_ENV = 'development';
  
  // Always works
  const a = require('../src/a');
  // Only when environment variables are passed in via the CLI
  import b from '../src/b';
  
  console.log('Bye bye..');  
  </code></pre>
  
  <p><strong>src/a.js</strong>  </p>
  
  <pre><code>'use strict'
  
  console.log('Module A:', process.env.NODE_ENV);
  
  const a = { name: "Module A" };
  export default a;
  </code></pre>
  
  <p><strong>src/b.js</strong>  </p>
  
  <pre><code>'use strict'
  
  console.log('Module B:', process.env.NODE_ENV);
  
  const b = { name: "Module B" };
  export default b;
  </code></pre>
  
  <h1>Running the code</h1>
  
  <p>Below you will see the output of both <code>npm</code> scripts:</p>
  
  <pre><code>npm run good
  # Outputs:
  Module B: development
  Module A: development
  Bye bye..
  
  npm run bad
  # Outputs:
  Module B: undefined
  Module A: development
  Bye bye..
  </code></pre>
  
  <h1>My question(s)</h1>
  
  <ol>
  <li>Why aren't programmatically set environment variables passed through to imported modules?</li>
  <li>Can this be fixed while keeping es2015 syntax? (e.g. using a babel plugin?)  </li>
  </ol>
  
  <h1>More info</h1>
  
  <p>Just moving my <code>process.env.NODE_PATH</code> over to the CLI won't work, <code>create-react-app</code> programmatically sets environment variables at multiple places in their configuration/script files. I have listed a few links below, pointing to the <code>create-react-app</code> repo and some of the files that are giving me troubles.</p>
  
  <ul>
  <li><a href="https://github.com/facebook/create-react-app" rel="nofollow noreferrer">create-react-app</a> A link to the <code>create-react-app</code> repo.</li>
  <li><a href="https://github.com/facebook/create-react-app/blob/next/packages/react-scripts/scripts/start.js" rel="nofollow noreferrer">scripts/start.js</a> This script sets both <code>process.env.NODE_ENV</code> and <code>process.env.BABEL_ENV</code>.</li>
  <li><a href="https://github.com/facebook/create-react-app/blob/next/packages/react-scripts/config/env.js" rel="nofollow noreferrer">config/env.js</a> This config file sets <code>process.env.NODE_PATH</code>.  </li>
  </ul>
  
  <h1>Note(s)</h1>
  
  <p>From my current understanding, <code>create-react-app</code> has little to none to do with the problem I'm having. I'm interested in why programmatically set environment variables are not passed through to imported modules.</p>
  
  <h1>My setup</h1>
  
  <ul>
  <li>OS: Ubuntu 16.04</li>
  <li>Node: v8.11.2</li>
  <li>Npm: 6.3</li>
  </ul>