How to use multiple .env files like .env.development with Express.jsWriting files in Node.jsHow do I pass command line arguments to a Node.js program?How to access environment variable values?How to decide when to use Node.js?How to exit in Node.jsHow do I delete an exported environment variable?How to get GET (query string) variables in Express.js on Node.js?What is Express.js?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?can't access .env with node-config

Replacing cord for IBM model M keyboard

Can you cure a Gorgon's Petrifying Breath before it finishes turning a target to stone?

I transpose the source code, you transpose the input!

How to level a picture frame hung on a single nail?

What organs or modifications would be needed to have hairy fish?

How do I automatically add linebreaks "\" to the end of each row of a tabular environment?

Avoiding dust scattering when you drill

How to say "respectively" in German when listing (enumerating) things

Windows 10 deletes lots of tiny files super slowly. Anything that can be done to speed it up?

How do I introduce dark themes?

Would a 737 pilot use flaps in nose dive?

Diminished data rate with logic output optoisolator

How to identify whether a publisher is genuine or not?

Worlds with different mathematics and logic

Why most footers have a background color has a divider of section?

Calculate the Ultraradical

How to bring home documents from work?

Can an energy drink or chocolate before an exam be useful ? What sort of other edible goods be helpful?

To what degree did the Supreme Court limit Boris Johnson's ability to prorogue?

Incomplete iffalse: How to shift a scope in polar coordinate?

Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?

does 'java' command compile the java program?

GPLv3 forces us to make code available, but to who?

How big would the ice ball have to be to deliver all the water at once?



How to use multiple .env files like .env.development with Express.js


Writing files in Node.jsHow do I pass command line arguments to a Node.js program?How to access environment variable values?How to decide when to use Node.js?How to exit in Node.jsHow do I delete an exported environment variable?How to get GET (query string) variables in Express.js on Node.js?What is Express.js?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?can't access .env with node-config






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I want to use seperate .env files for each mode (development, production, etc...). When working on my vue.js projects, i can use files like .env.development or .env.production to get diffenent values for the same env key. (example: in .env.development: FOO=BAR and in .env.production: FOO=BAZ, in development mode process.env.FOO would be BAR, in production i'd be BAZ).



I'm working on an Express server and want to use these same kinds of .env files to store the port, db uri, user, pwd...



I know i can edit the scripts in package.json like



"scripts": 
"start": "NODE_ENV=development PORT=80 node ./bin/www",
"start-prod": "NODE_ENV=production PORT=81 node ./bin/www"



but this gets messy when using multiple variables.



I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.



Can i use the dotenv package or do i need another one? Or could i do this withouth any package at all?










share|improve this question



















  • 1





    Welcome to Stack Overflow. Why would you need multiple environment files on one host? Normally your development and production environments are on different hosts. (dev on your computer and production in the cloud for example).

    – Varcorb
    Mar 28 at 20:18











  • npmjs.com/package/dotenv#should-i-have-multiple-env-files

    – lifeisfoo
    Mar 28 at 20:24

















1















I want to use seperate .env files for each mode (development, production, etc...). When working on my vue.js projects, i can use files like .env.development or .env.production to get diffenent values for the same env key. (example: in .env.development: FOO=BAR and in .env.production: FOO=BAZ, in development mode process.env.FOO would be BAR, in production i'd be BAZ).



I'm working on an Express server and want to use these same kinds of .env files to store the port, db uri, user, pwd...



I know i can edit the scripts in package.json like



"scripts": 
"start": "NODE_ENV=development PORT=80 node ./bin/www",
"start-prod": "NODE_ENV=production PORT=81 node ./bin/www"



but this gets messy when using multiple variables.



I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.



Can i use the dotenv package or do i need another one? Or could i do this withouth any package at all?










share|improve this question



















  • 1





    Welcome to Stack Overflow. Why would you need multiple environment files on one host? Normally your development and production environments are on different hosts. (dev on your computer and production in the cloud for example).

    – Varcorb
    Mar 28 at 20:18











  • npmjs.com/package/dotenv#should-i-have-multiple-env-files

    – lifeisfoo
    Mar 28 at 20:24













1












1








1


1






I want to use seperate .env files for each mode (development, production, etc...). When working on my vue.js projects, i can use files like .env.development or .env.production to get diffenent values for the same env key. (example: in .env.development: FOO=BAR and in .env.production: FOO=BAZ, in development mode process.env.FOO would be BAR, in production i'd be BAZ).



I'm working on an Express server and want to use these same kinds of .env files to store the port, db uri, user, pwd...



I know i can edit the scripts in package.json like



"scripts": 
"start": "NODE_ENV=development PORT=80 node ./bin/www",
"start-prod": "NODE_ENV=production PORT=81 node ./bin/www"



but this gets messy when using multiple variables.



I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.



Can i use the dotenv package or do i need another one? Or could i do this withouth any package at all?










share|improve this question














I want to use seperate .env files for each mode (development, production, etc...). When working on my vue.js projects, i can use files like .env.development or .env.production to get diffenent values for the same env key. (example: in .env.development: FOO=BAR and in .env.production: FOO=BAZ, in development mode process.env.FOO would be BAR, in production i'd be BAZ).



I'm working on an Express server and want to use these same kinds of .env files to store the port, db uri, user, pwd...



I know i can edit the scripts in package.json like



"scripts": 
"start": "NODE_ENV=development PORT=80 node ./bin/www",
"start-prod": "NODE_ENV=production PORT=81 node ./bin/www"



but this gets messy when using multiple variables.



I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.



Can i use the dotenv package or do i need another one? Or could i do this withouth any package at all?







node.js express environment-variables dotenv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 20:08









JonasJonas

85 bronze badges




85 bronze badges










  • 1





    Welcome to Stack Overflow. Why would you need multiple environment files on one host? Normally your development and production environments are on different hosts. (dev on your computer and production in the cloud for example).

    – Varcorb
    Mar 28 at 20:18











  • npmjs.com/package/dotenv#should-i-have-multiple-env-files

    – lifeisfoo
    Mar 28 at 20:24












  • 1





    Welcome to Stack Overflow. Why would you need multiple environment files on one host? Normally your development and production environments are on different hosts. (dev on your computer and production in the cloud for example).

    – Varcorb
    Mar 28 at 20:18











  • npmjs.com/package/dotenv#should-i-have-multiple-env-files

    – lifeisfoo
    Mar 28 at 20:24







1




1





Welcome to Stack Overflow. Why would you need multiple environment files on one host? Normally your development and production environments are on different hosts. (dev on your computer and production in the cloud for example).

– Varcorb
Mar 28 at 20:18





Welcome to Stack Overflow. Why would you need multiple environment files on one host? Normally your development and production environments are on different hosts. (dev on your computer and production in the cloud for example).

– Varcorb
Mar 28 at 20:18













npmjs.com/package/dotenv#should-i-have-multiple-env-files

– lifeisfoo
Mar 28 at 20:24





npmjs.com/package/dotenv#should-i-have-multiple-env-files

– lifeisfoo
Mar 28 at 20:24












1 Answer
1






active

oldest

votes


















1

















I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.




Not true -- see the path option.



For example, require('dotenv').config( path: `/path/to/.env.$process.env.NODE_ENV` )






share|improve this answer

























  • For anyone else: i finally went with this: const path = require('path'); require('dotenv').config( path: path.join(__dirname, ../.env.$process.env.NODE_ENV));

    – Jonas
    Mar 30 at 9:41














Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406055%2fhow-to-use-multiple-env-files-like-env-development-with-express-js%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1

















I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.




Not true -- see the path option.



For example, require('dotenv').config( path: `/path/to/.env.$process.env.NODE_ENV` )






share|improve this answer

























  • For anyone else: i finally went with this: const path = require('path'); require('dotenv').config( path: path.join(__dirname, ../.env.$process.env.NODE_ENV));

    – Jonas
    Mar 30 at 9:41
















1

















I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.




Not true -- see the path option.



For example, require('dotenv').config( path: `/path/to/.env.$process.env.NODE_ENV` )






share|improve this answer

























  • For anyone else: i finally went with this: const path = require('path'); require('dotenv').config( path: path.join(__dirname, ../.env.$process.env.NODE_ENV));

    – Jonas
    Mar 30 at 9:41














1














1










1










I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.




Not true -- see the path option.



For example, require('dotenv').config( path: `/path/to/.env.$process.env.NODE_ENV` )






share|improve this answer














I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.




Not true -- see the path option.



For example, require('dotenv').config( path: `/path/to/.env.$process.env.NODE_ENV` )







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 29 at 23:18









MarlornMarlorn

3312 silver badges14 bronze badges




3312 silver badges14 bronze badges















  • For anyone else: i finally went with this: const path = require('path'); require('dotenv').config( path: path.join(__dirname, ../.env.$process.env.NODE_ENV));

    – Jonas
    Mar 30 at 9:41


















  • For anyone else: i finally went with this: const path = require('path'); require('dotenv').config( path: path.join(__dirname, ../.env.$process.env.NODE_ENV));

    – Jonas
    Mar 30 at 9:41

















For anyone else: i finally went with this: const path = require('path'); require('dotenv').config( path: path.join(__dirname, ../.env.$process.env.NODE_ENV));

– Jonas
Mar 30 at 9:41






For anyone else: i finally went with this: const path = require('path'); require('dotenv').config( path: path.join(__dirname, ../.env.$process.env.NODE_ENV));

– Jonas
Mar 30 at 9:41





















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406055%2fhow-to-use-multiple-env-files-like-env-development-with-express-js%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript