title: Why are my custom process.env not working within dotenv?
body: <p>Learning that it is a bad practice to include API secret keys I've done some research and trying to learn how to create custom <code>process.env</code>.</p>
  
  <p>After reading:</p>
  
  <ul>
  <li><a href="https://medium.com/the-node-js-collection/making-your-node-js-work-everywhere-with-environment-variables-2da8cdf6e786" rel="noreferrer">Node.js Everywhere with Environment Variables!</a></li>
  <li><a href="https://stackoverflow.com/questions/9198310/how-to-set-node-env-to-production-development-in-os-x">How to set NODE_ENV to production/development in OS X</a></li>
  <li><a href="https://stackoverflow.com/questions/42385158/how-to-set-process-env-from-the-file-in-nodejs">How to set process.env from the file in NodeJS?</a></li>
  <li><a href="https://stackoverflow.com/questions/42335016/dotenv-file-is-not-loading-environment-variables">dotenv file is not loading environment variables</a></li>
  </ul>
  
  <p>I'm trying to set an env file locally based on <code>process.env.NODE_ENV</code>. The application would be hosted on Heroku and in my .gitignore I have <code>dev.env</code>  but when I try to use <a href="https://www.npmjs.com/package/dotenv" rel="noreferrer"><code>dotenv</code></a> locally I'm getting an <code>undefined</code>. I have set the environment locally with <code>export NODE_ENV=development</code> in my terminal. When I run the command <code>npm start</code> or <code>nodemon</code> both return <code>undefined</code> but in <em>env.js</em> I get <code>Testing for: development</code>, example:</p>
  
  <pre><code>nodemon
  
  [nodemon] 1.19.1
  [nodemon] to restart at any time, enter `rs`
  [nodemon] watching: *.*
  [nodemon] starting `node app.js`
  Testing for: development
  undefined
  </code></pre>
  
  <p>Here is what I have:</p>
  
  <p><em>app.js</em>:</p>
  
  <pre><code>const keys = require('./config/env')
  return console.log(process.env.PORT)
  </code></pre>
  
  <p><em>config/env.js</em>:</p>
  
  <pre><code>const env = process.env.NODE_ENV
  console.log(`Testing for: ${env}`)
  try {
    switch(env) {
      case 'undefined':
        Error('Environment undefined, if local in terminal: export NODE_ENV=development')
        break
      case 'development':
        require('dotenv').config({
          path: './dev.env'
        })
        break
      case 'production':
        require('dotenv').config({
          path: './prod.env'
        })
        break
      default:
        Error('Unrecognized Environment')
    }  
  } catch (err) {
    Error('Error trying to run file')
  }
  </code></pre>
  
  <p><em>config/dev.env</em>:</p>
  
  <pre><code>## Port number to run Application
  PORT=4321
  </code></pre>
  
  <p>but in <em>app.js</em> when I test with <code>return console.log(process.env.PORT)</code> or <code>return console.log(keys.PORT)</code> they both log <code>undefined</code>, why? I seem to be doing something wrong in <em>env.js</em> when using <code>dotenv</code>. </p>
  
  <p>To clarify I'm not even pushing to Heroku yet and <em>prod.env</em> will be validation. If there is a better approach please educate me.</p>