Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my custom process.env not working within dotenv?

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 process.env.

After reading:

  • Node.js Everywhere with Environment Variables!
  • How to set NODE_ENV to production/development in OS X
  • How to set process.env from the file in NodeJS?
  • dotenv file is not loading environment variables

I'm trying to set an env file locally based on process.env.NODE_ENV. The application would be hosted on Heroku and in my .gitignore I have dev.env but when I try to use dotenv locally I'm getting an undefined. I have set the environment locally with export NODE_ENV=development in my terminal. When I run the command npm start or nodemon both return undefined but in env.js I get Testing for: development, example:

nodemon

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
Testing for: development
undefined

Here is what I have:

app.js:

const keys = require('./config/env')
return console.log(process.env.PORT)

config/env.js:

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')
}

config/dev.env:

## Port number to run Application
PORT=4321

but in app.js when I test with return console.log(process.env.PORT) or return console.log(keys.PORT) they both log undefined, why? I seem to be doing something wrong in env.js when using dotenv.

To clarify I'm not even pushing to Heroku yet and prod.env will be validation. If there is a better approach please educate me.

like image 437
DᴀʀᴛʜVᴀᴅᴇʀ Avatar asked Jul 26 '19 04:07

DᴀʀᴛʜVᴀᴅᴇʀ


People also ask

Does dotenv override environment variables?

It seems like dotenv does not override variables if they are defined in the environment, by design: By default, it won't overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does.

What does dotenv config () do?

DotEnv is a lightweight npm package that automatically loads environment variables from a . env file into the process.

Does dotenv work in production?

The reason you dont use it in production is that in production you would usually be running the application in a docker container or a dedicated server, either of which, you wouldnt need to worry about setting conflicting environment variables.


2 Answers

I've figured where I was going wrong after re-reading the documentation regarding path, example:

require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })

After changing:

case 'development':
  require('dotenv').config({
    path: './dev.env'
  })
  break

to:

case 'development':
  require('dotenv').config({
    path: `${__dirname}/dev.env`
  })
  break

it works. So my error was a scope issue. No need to set const keys so just using require('./config/env') I can access any custom processes, example:

process.env.CUSTOM

or in this case it would be:

process.env.PORT

from app.js

like image 153
DᴀʀᴛʜVᴀᴅᴇʀ Avatar answered Sep 24 '22 19:09

DᴀʀᴛʜVᴀᴅᴇʀ


Hi at first use switch(env.trim()), then change the path value path: './config/dev.env'. Goodluck.

like image 31
Stanislav Baturin Avatar answered Sep 22 '22 19:09

Stanislav Baturin