Toggle between multiple .env files like .env.development with node.js

You can specify which .env file path to use via the path option with something like this:

require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` })

I just wanted to add this for other people who are having issues. The above two answers are both slightly off. let me explain. The guy above had to use path as in

  • the first example it's missing a ./ the ./ indicates current directory to Node.js. Thats why in the first example if the env file was not in the root of the PC it would never find it. Using path is a nasty work around but the best way to do it is to simply add a ./ this says "hey computer look in my current directory for this file". - thanks to David J. Malan
//this says hey computer look in my current directory for this file. 
 
require('dotenv').config({ path: `./.env.${process.env.NODE_ENV}` })


//I reccomend doing a console.log as well to make sure the names match*
console.log(`./.env.${process.env.NODE_ENV}`)