title: NodeJS dynamically environment variable setup
body: <p>I create a NodeJS server and trying to setup environment variables dynamically. But I'm facing a weird problem. I create a <code>env.js</code> file and here is the related code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const environment = {}
  
  environment.dev = {
    port: 3000,
    envName: 'dev'
  }
  
  environment.prod = {
    port: 5000,
    envName: 'prod'
  }
  
  const currentEnvironment = typeof process.env.NODE_ENV === 'string'
    ? process.env.NODE_ENV
    : 'dev'
  
  console.log('current environment: ', currentEnvironment) // get currentEnvironment: prod
  console.log('environment obj: ', environment) // get environment obj correctly
  console.log('getting obj from environment obj: ', environment[currentEnvironment]) // But get undefined
  
  const environmentToExport = typeof environment[currentEnvironment] === 'object'
    ? environment[currentEnvironment]
    : environment.dev
  
  module.exports = environmentToExport</code></pre>
  </div>
  </div>
  </p>
  <p>I can't figure out why I'm getting <code>undefined</code> in <code>environment[currentEnvironment]</code>.</p>
  <p>And here is the <code>package.json</code> scripts from where I'm passing environment variable. I'm using <code>windows</code> by the way.</p>
  <pre><code>&quot;scripts&quot;: {
    &quot;dev&quot;: &quot;SET NODE_ENV=dev &amp; nodemon index&quot;,
    &quot;prod&quot;: &quot;SET NODE_ENV=prod &amp; nodemon index&quot;
  }
  </code></pre>
  <p>Note: I'm using exported <code>port</code> number while creating server. But incase of getting <code>undefined</code>, I'm always exporting <code>dev</code> environment ! Hope you understand my problem.</p>