Dotenv: Can .env file extend another .env file ?

Created on 14 Feb 2019  Â·  4Comments  Â·  Source: motdotla/dotenv

like this:
in my src/tslint.json
{
"extends": "base-tslint.json", //extends another one
"no-any": true,
"prefer-const": false,
}

Most helpful comment

Because dotenv is just assigning keys that don’t already exist in process.env, you can just call config multiple times.

const dotenv = require(“dotenv”)

[“.env”, “.env.whatever”].forEach(path => dotenv.config({ path }))

All 4 comments

actually, i'm curious as to why the answer to this Should I have multiple .env files? is No. It seems to go against common practice. As devs we need the flexibility of having env.local, env.staging, etc.

If anyone can elaborate on how else to go about it.

@cjaoude It's in this page linked to from the README: https://12factor.net/config

Because dotenv is just assigning keys that don’t already exist in process.env, you can just call config multiple times.

const dotenv = require(“dotenv”)

[“.env”, “.env.whatever”].forEach(path => dotenv.config({ path }))

@maxbeatty smart idea. But as note that first files take precedence. My version:

// withEnv.js
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');

[ '.env.local','.env.development','.env']
    .forEach(name => fs.existsSync(path.resolve(__dirname,name)) && dotenv.config({ path: name }) );

And real usage in package.json:

{
    "start": "node -r ./withEnv  ./node_modules/.bin/react-native start",
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shai32 picture shai32  Â·  5Comments

awesomejerry picture awesomejerry  Â·  4Comments

ycmjason picture ycmjason  Â·  4Comments

DesignByOnyx picture DesignByOnyx  Â·  4Comments

goldbergyoni picture goldbergyoni  Â·  3Comments