18

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:

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.

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

5 Answers5

20

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

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
  • For future reference, dotenv will return an error if it can't find or read the file you specify with the `path` option. We surface errors to help with debugging like this. – maxbeatty Aug 09 '19 at 16:19
1

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

0

You are getting this error because you need to set those environment variables, as we don't have .env files there whereas on dev server dotenv set it for you using your .env file.

You need to set those variables either via the web interface or the heroku CLI. This heroku doc will help you.

Aakash
  • 66
  • 1
  • 7
  • I know how to set in the CLI the issue is using the custom process in development. Think you’re misunderstanding the question. I can log the NODE_ENV so the error is from env.js to dotenv. – DᴀʀᴛʜVᴀᴅᴇʀ Jul 26 '19 at 05:46
  • as I have looked, `config()` takes path to directory where file named **.env** is located. So that's one problem. What we do is replace .env with the file .env. before build. – Aakash Jul 26 '19 at 06:37
  • And that is why in *env.js* I'm setting the path in `config` with `path: './dev.env'`. I may be doing this wrong but per the documentation that is the correct way to declare a file if not using `.env`. – DᴀʀᴛʜVᴀᴅᴇʀ Jul 26 '19 at 12:54
0

Heroku doesn't run on dotenv.

It has something called "Config Vars" within the Settings page of the given Heroku App:

For example

Heroku Config Vars

For your example, if the code wants to access: process.env.NODE_ENV, you would set a new Config Var within your Heroku Settings page of the given app, and call it NODE_ENV. Set the value you want, and press save. Voila. Done.

The added value of dotenv is usually for testing on your local machine.

0

if you are seeing the variables using echo $var but process.env.var is giving you undefined, then make sure you have that export keyword when creating the variable. export var="value"

Rstar37
  • 454
  • 5
  • 6