I want to start by saying "thanks" to the contributors as I think this library is great.
Now, my question is different from #220.
I'm starting on a dummy project, and I want to add some integration testing using https://github.com/visionmedia/supertest. I will definitely need to load different env variables for the development env, which I access through the browser; and the test env, which mocha runs. Different mongodb urls, Etc.
I'm not talking about committing production/qa envs, but local envs for different environments (I think this are also called "flavors"?)
What approach could I take here?
Also, if this thread becomes useful and informative, I'd like to request for it to be put in the FAQ README section, as I think many people could benefit from it.
Thanks in advance
Solving for this is really up to personal preference.
You could accomplish it via the file system by symlinking files.
ln -s .env.dev .env
rm .env
ln -s .env.test .env
You could conditionally load files based on an _environment variable_.
const dotenv = require("dotenv")
const envFile = process.env.NODE_ENV ? `.env.${process.env.NODE_END}` : '.env'
dotenv.config({ path: envFile })
@maxbeatty This is similar to the approach I had thought before I read your recommendation of not having multiple .env files.
IMHO the library should do it.
In config folder I have two files prod.env and dev.env. I am Starting node with the command ENVIRONMENT=dev node index.js or ENVIRONMENT=prod node index.js. Below is implementation.
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname, ../config/${process.env.ENVIRONMENT}.env)});
In
configfolder I have two filesprod.envanddev.env. I am Startingnodewith the commandENVIRONMENT=dev node index.jsorENVIRONMENT=prod node index.js. Below is implementation.import dotenv from 'dotenv';
import path from 'path';dotenv.config({ path: path.resolve(__dirname,
../config/${process.env.ENVIRONMENT}.env)});
For people working with Javascript , replace ${variableName} with +variableName followed by 'string'
In
configfolder I have two filesprod.envanddev.env. I am Startingnodewith the commandENVIRONMENT=dev node index.jsorENVIRONMENT=prod node index.js. Below is implementation.import dotenv from 'dotenv';
import path from 'path';dotenv.config({ path: path.resolve(__dirname,
../config/${process.env.ENVIRONMENT}.env)});
The best solution, love it
For people working with Javascript , replace ${variableName} with +variableName followed by 'string'
@hajimurtaza that is javascript, but the blockquote formatting is making the backticks render as code instead of leaving them as literals. It should read as:
dotenv.config({ path: path.resolve(__dirname, `../config/${process.env.ENVIRONMENT}.env`)});
The backticks are template literals.
Solutions above didn't work for me. I am using Windows 10. Ended up with creating cli argument parse method, that i gets invoked in the main file (index.js or whatever you like). This solution is universal. I would like to share with you, guys, in case someone has the similar problem. _CommonJS module syntax is used_
File: parse-cli.js
Description: returns flag value based on the flag name (--[flagName]=[flagValue]), undefined otherwise. It should have at least single dash at the beginning.
exports.parseCliFlagValue = (flagName) => {
const flag = process.argv.find(argument => argument.indexOf(`-${flagName}=`) > -1);
if (flag) {
return flag.slice(flag.indexOf('=') + 1);
}
return undefined;
};
File: index.js (or whatever your main js file is)
Description: includes dotenv config dynamically based on a cli argument passed
const {parseCliFlagValue} = require('./utils/parse-cli-flags');
const environment = parseCliFlagValue('env');
require('dotenv').config({path: `src/environments/.env.${environment}`}); // amend the last part in case you have another config name format
Now you can call any of these:
nodemon src/server.js --env=dev // will include dev dotenv config. chiefly used in development mode
or
node src/server.js --env=prod // for production mode
Most helpful comment
In
configfolder I have two filesprod.envanddev.env. I am Startingnodewith the commandENVIRONMENT=dev node index.jsorENVIRONMENT=prod node index.js. Below is implementation.import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname,
../config/${process.env.ENVIRONMENT}.env)});