Code Monkey home page Code Monkey logo

dotenv's Introduction

๐ŸŽ‰ announcing dotenvx. run anywhere, multi-environment, encrypted envs.

ย 

dotenv NPM version

dotenv

Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.

js-standard-style LICENSE codecov

๐ŸŒฑ Install

# install locally (recommended)
npm install dotenv --save

Or installing with yarn? yarn add dotenv

๐Ÿ—๏ธ Usage

how to use dotenv video tutorial youtube/@dotenvorg

Create a .env file in the root of your project (if using a monorepo structure like apps/backend/app.js, put it in the root of the folder where your app.js process runs):

S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"

As early as possible in your application, import and configure dotenv:

require('dotenv').config()
console.log(process.env) // remove this after you've confirmed it is working

.. or using ES6?

import 'dotenv/config'

That's it. process.env now has the keys and values you defined in your .env file:

require('dotenv').config()

...

s3.getBucketCors({Bucket: process.env.S3_BUCKET}, function(err, data) {})

Multiline values

If you need multiline variables, for example private keys, those are now supported (>= v15.0.0) with line breaks:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
...
Kh9NV...
...
-----END RSA PRIVATE KEY-----"

Alternatively, you can double quote strings and use the \n character:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n"

Comments

Comments may be added to your file on their own line or inline:

# This is a comment
SECRET_KEY=YOURSECRETKEYGOESHERE # comment
SECRET_HASH="something-with-a-#-hash"

Comments begin where a # exists, so if your value contains a # please wrap it in quotes. This is a breaking change from >= v15.0.0 and on.

Parsing

The engine which parses the contents of your file containing environment variables is available to use. It accepts a String or Buffer and will return an Object with the parsed keys and values.

const dotenv = require('dotenv')
const buf = Buffer.from('BASIC=basic')
const config = dotenv.parse(buf) // will return an object
console.log(typeof config, config) // object { BASIC : 'basic' }

Preload

Note: Consider using dotenvx instead of preloading. I am now doing (and recommending) so.

It serves the same purpose (you do not need to require and load dotenv), adds better debugging, and works with ANY language, framework, or platform. โ€“ motdotla

You can use the --require (-r) command line option to preload dotenv. By doing this, you do not need to require and load dotenv in your application code.

$ node -r dotenv/config your_script.js

The configuration options below are supported as command line arguments in the format dotenv_config_<option>=value

$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env dotenv_config_debug=true

Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.

$ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
$ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env

Variable Expansion

You need to add the value of another variable in one of your variables? Use dotenv-expand.

Syncing

You need to keep .env files in sync between machines, environments, or team members? Use dotenv-vault.

Multiple Environments

You need to manage your secrets across different environments and apply them as needed? Use a .env.vault file with a DOTENV_KEY.

Deploying

You need to deploy your secrets in a cloud-agnostic manner? Use a .env.vault file. See deploying .env.vault files.

๐ŸŒด Manage Multiple Environments

Use dotenvx or dotenv-vault.

dotenvx

Run any environment locally. Create a .env.ENVIRONMENT file and use --env-file to load it. It's straightforward, yet flexible.

$ echo "HELLO=production" > .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run --env-file=.env.production -- node index.js
Hello production
> ^^

or with multiple .env files

$ echo "HELLO=local" > .env.local
$ echo "HELLO=World" > .env
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run --env-file=.env.local --env-file=.env -- node index.js
Hello local

more environment examples

dotenv-vault

Edit your production environment variables.

$ npx dotenv-vault open production

Regenerate your .env.vault file.

$ npx dotenv-vault build

โ„น๏ธ ๐Ÿ” Vault Managed vs ๐Ÿ’ป Locally Managed: The above example, for brevity's sake, used the ๐Ÿ” Vault Managed solution to manage your .env.vault file. You can instead use the ๐Ÿ’ป Locally Managed solution. Read more here. Our vision is that other platforms and orchestration tools adopt the .env.vault standard as they did the .env standard. We don't expect to be the only ones providing tooling to manage and generate .env.vault files.

Learn more at dotenv-vault: Manage Multiple Environments

๐Ÿš€ Deploying

Use dotenvx or dotenv-vault.

dotenvx

Encrypt your secrets to a .env.vault file and load from it (recommended for production and ci).

$ echo "HELLO=World" > .env
$ echo "HELLO=production" > .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx encrypt
[dotenvx][info] encrypted to .env.vault (.env,.env.production)
[dotenvx][info] keys added to .env.keys (DOTENV_KEY_PRODUCTION,DOTENV_KEY_PRODUCTION)

$ DOTENV_KEY='<dotenv_key_production>' dotenvx run -- node index.js
[dotenvx][info] loading env (1) from encrypted .env.vault
Hello production
^ :-]

learn more

dotenv-vault

Note: Requires dotenv >= 16.1.0

Encrypt your .env.vault file.

$ npx dotenv-vault build

Fetch your production DOTENV_KEY.

$ npx dotenv-vault keys production

Set DOTENV_KEY on your server.

# heroku example
heroku config:set DOTENV_KEY=dotenv://:key_1234โ€ฆ@dotenvx.com/vault/.env.vault?environment=production

That's it! On deploy, your .env.vault file will be decrypted and its secrets injected as environment variables โ€“ just in time.

โ„น๏ธ A note from Mot: Until recently, we did not have an opinion on how and where to store your secrets in production. We now strongly recommend generating a .env.vault file. It's the best way to prevent your secrets from being scattered across multiple servers and cloud providers โ€“ protecting you from breaches like the CircleCI breach. Also it unlocks interoperability WITHOUT native third-party integrations. Third-party integrations are increasingly risky to our industry. They may be the 'du jour' of today, but we imagine a better future.

Learn more at dotenv-vault: Deploying

๐Ÿ“š Examples

See examples of using dotenv with various frameworks, languages, and configurations.

๐Ÿ“– Documentation

Dotenv exposes four functions:

  • config
  • parse
  • populate
  • decrypt

Config

config will read your .env file, parse the contents, assign it to process.env, and return an Object with a parsed key containing the loaded content or an error key if it failed.

const result = dotenv.config()

if (result.error) {
  throw result.error
}

console.log(result.parsed)

You can additionally, pass options to config.

Options

path

Default: path.resolve(process.cwd(), '.env')

Specify a custom path if your file containing environment variables is located elsewhere.

require('dotenv').config({ path: '/custom/path/to/.env' })

By default, config will look for a file called .env in the current working directory.

Pass in multiple files as an array, and they will be parsed in order and combined with process.env (or option.processEnv, if set). The first value set for a variable will win, unless the options.override flag is set, in which case the last value set will win. If a value already exists in process.env and the options.override flag is NOT set, no changes will be made to that value.

require('dotenv').config({ path: ['.env.local', '.env'] })
encoding

Default: utf8

Specify the encoding of your file containing environment variables.

require('dotenv').config({ encoding: 'latin1' })
debug

Default: false

Turn on logging to help debug why certain keys or values are not being set as you expect.

require('dotenv').config({ debug: process.env.DEBUG })
override

Default: false

Override any environment variables that have already been set on your machine with values from your .env file(s). If multiple files have been provided in option.path the override will also be used as each file is combined with the next. Without override being set, the first value wins. With override set the last value wins.

require('dotenv').config({ override: true })
processEnv

Default: process.env

Specify an object to write your secrets to. Defaults to process.env environment variables.

const myObject = {}
require('dotenv').config({ processEnv: myObject })

console.log(myObject) // values from .env or .env.vault live here now.
console.log(process.env) // this was not changed or written to
DOTENV_KEY

Default: process.env.DOTENV_KEY

Pass the DOTENV_KEY directly to config options. Defaults to looking for process.env.DOTENV_KEY environment variable. Note this only applies to decrypting .env.vault files. If passed as null or undefined, or not passed at all, dotenv falls back to its traditional job of parsing a .env file.

require('dotenv').config({ DOTENV_KEY: 'dotenv://:key_1234โ€ฆ@dotenvx.com/vault/.env.vault?environment=production' })

Parse

The engine which parses the contents of your file containing environment variables is available to use. It accepts a String or Buffer and will return an Object with the parsed keys and values.

const dotenv = require('dotenv')
const buf = Buffer.from('BASIC=basic')
const config = dotenv.parse(buf) // will return an object
console.log(typeof config, config) // object { BASIC : 'basic' }

Options

debug

Default: false

Turn on logging to help debug why certain keys or values are not being set as you expect.

const dotenv = require('dotenv')
const buf = Buffer.from('hello world')
const opt = { debug: true }
const config = dotenv.parse(buf, opt)
// expect a debug message because the buffer is not in KEY=VAL form

Populate

The engine which populates the contents of your .env file to process.env is available for use. It accepts a target, a source, and options. This is useful for power users who want to supply their own objects.

For example, customizing the source:

const dotenv = require('dotenv')
const parsed = { HELLO: 'world' }

dotenv.populate(process.env, parsed)

console.log(process.env.HELLO) // world

For example, customizing the source AND target:

const dotenv = require('dotenv')
const parsed = { HELLO: 'universe' }
const target = { HELLO: 'world' } // empty object

dotenv.populate(target, parsed, { override: true, debug: true })

console.log(target) // { HELLO: 'universe' }

options

Debug

Default: false

Turn on logging to help debug why certain keys or values are not being populated as you expect.

override

Default: false

Override any environment variables that have already been set.

Decrypt

The engine which decrypts the ciphertext contents of your .env.vault file is available for use. It accepts a ciphertext and a decryption key. It uses AES-256-GCM encryption.

For example, decrypting a simple ciphertext:

const dotenv = require('dotenv')
const ciphertext = 's7NYXa809k/bVSPwIAmJhPJmEGTtU0hG58hOZy7I0ix6y5HP8LsHBsZCYC/gw5DDFy5DgOcyd18R'
const decryptionKey = 'ddcaa26504cd70a6fef9801901c3981538563a1767c297cb8416e8a38c62fe00'

const decrypted = dotenv.decrypt(ciphertext, decryptionKey)

console.log(decrypted) // # development@v6\nALPHA="zeta"

โ“ FAQ

Why is the .env file not loading my environment variables successfully?

Most likely your .env file is not in the correct place. See this stack overflow.

Turn on debug mode and try again..

require('dotenv').config({ debug: true })

You will receive a helpful error outputted to your console.

Should I commit my .env file?

No. We strongly recommend against committing your .env file to version control. It should only include environment-specific values such as database passwords or API keys. Your production database should have a different password than your development database.

Should I have multiple .env files?

We recommend creating on .env file per environment. Use .env for local/development, .env.production for production and so on. This still follows the twelve factor principles as each is attributed individually to its own environment. Avoid custom set ups that work in inheritance somehow (.env.production inherits values form .env for example). It is better to duplicate values if necessary across each .env.environment file.

In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as โ€œenvironmentsโ€, but instead are independently managed for each deploy. This is a model that scales up smoothly as the app naturally expands into more deploys over its lifetime.

โ€“ The Twelve-Factor App

What rules does the parsing engine follow?

The parsing engine currently supports the following rules:

  • BASIC=basic becomes {BASIC: 'basic'}
  • empty lines are skipped
  • lines beginning with # are treated as comments
  • # marks the beginning of a comment (unless when the value is wrapped in quotes)
  • empty values become empty strings (EMPTY= becomes {EMPTY: ''})
  • inner quotes are maintained (think JSON) (JSON={"foo": "bar"} becomes {JSON:"{\"foo\": \"bar\"}")
  • whitespace is removed from both ends of unquoted values (see more on trim) (FOO= some value becomes {FOO: 'some value'})
  • single and double quoted values are escaped (SINGLE_QUOTE='quoted' becomes {SINGLE_QUOTE: "quoted"})
  • single and double quoted values maintain whitespace from both ends (FOO=" some value " becomes {FOO: ' some value '})
  • double quoted values expand new lines (MULTILINE="new\nline" becomes
{MULTILINE: 'new
line'}
  • backticks are supported (BACKTICK_KEY=`This has 'single' and "double" quotes inside of it.`)

What happens to environment variables that were already set?

By default, we will never modify any environment variables that have already been set. In particular, if there is a variable in your .env file which collides with one that already exists in your environment, then that variable will be skipped.

If instead, you want to override process.env use the override option.

require('dotenv').config({ override: true })

How come my environment variables are not showing up for React?

Your React code is run in Webpack, where the fs module or even the process global itself are not accessible out-of-the-box. process.env can only be injected through Webpack configuration.

If you are using react-scripts, which is distributed through create-react-app, it has dotenv built in but with a quirk. Preface your environment variables with REACT_APP_. See this stack overflow for more details.

If you are using other frameworks (e.g. Next.js, Gatsby...), you need to consult their documentation for how to inject environment variables into the client.

Can I customize/write plugins for dotenv?

Yes! dotenv.config() returns an object representing the parsed .env file. This gives you everything you need to continue setting values on process.env. For example:

const dotenv = require('dotenv')
const variableExpansion = require('dotenv-expand')
const myEnv = dotenv.config()
variableExpansion(myEnv)

How do I use dotenv with import?

Simply..

// index.mjs (ESM)
import 'dotenv/config' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
import express from 'express'

A little background..

When you run a module containing an import declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.

โ€“ ES6 In Depth: Modules

What does this mean in plain language? It means you would think the following would work but it won't.

errorReporter.mjs:

import { Client } from 'best-error-reporting-service'

export default new Client(process.env.API_KEY)

index.mjs:

// Note: this is INCORRECT and will not work
import * as dotenv from 'dotenv'
dotenv.config()

import errorReporter from './errorReporter.mjs'
errorReporter.report(new Error('documented example'))

process.env.API_KEY will be blank.

Instead, index.mjs should be written as..

import 'dotenv/config'

import errorReporter from './errorReporter.mjs'
errorReporter.report(new Error('documented example'))

Does that make sense? It's a bit unintuitive, but it is how importing of ES6 modules work. Here is a working example of this pitfall.

There are two alternatives to this approach:

  1. Preload dotenv: node --require dotenv/config index.js (Note: you do not need to import dotenv with this approach)
  2. Create a separate file that will execute config first as outlined in this comment on #133

Why am I getting the error Module not found: Error: Can't resolve 'crypto|os|path'?

You are using dotenv on the front-end and have not included a polyfill. Webpack < 5 used to include these for you. Do the following:

npm install node-polyfill-webpack-plugin

Configure your webpack.config.js to something like the following.

require('dotenv').config()

const path = require('path');
const webpack = require('webpack')

const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')

module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new NodePolyfillPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        HELLO: JSON.stringify(process.env.HELLO)
      }
    }),
  ]
};

Alternatively, just use dotenv-webpack which does this and more behind the scenes for you.

What about variable expansion?

Try dotenv-expand

What about syncing and securing .env files?

Use dotenv-vault

What is a .env.vault file?

A .env.vault file is an encrypted version of your development (and ci, staging, production, etc) environment variables. It is paired with a DOTENV_KEY to deploy your secrets more securely than scattering them across multiple platforms and tools. Use dotenv-vault to manage and generate them.

What if I accidentally commit my .env file to code?

Remove it, remove git history and then install the git pre-commit hook to prevent this from ever happening again.

brew install dotenvx/brew/dotenvx
dotenvx precommit --install

How can I prevent committing my .env file to a Docker build?

Use the docker prebuild hook.

# Dockerfile
...
RUN curl -fsS https://dotenvx.sh/ | sh
...
RUN dotenvx prebuild
CMD ["dotenvx", "run", "--", "node", "index.js"]

Contributing Guide

See CONTRIBUTING.md

CHANGELOG

See CHANGELOG.md

Who's using dotenv?

These npm modules depend on it.

Projects that expand it often use the keyword "dotenv" on npm.

dotenv's People

Contributors

amilajack avatar andreialecu avatar brelian avatar darkgl0w avatar deepam-kapur avatar franciscop avatar imrodry avatar jcblw avatar jessefulton avatar jonathan-reisdorf avatar kirtan-desai avatar linusu avatar litomore avatar lnasc256 avatar maxbeatty avatar metcoder95 avatar michaeldeboey avatar mmiszy avatar motdotla avatar mrsnoozles avatar nquinlan avatar palanik avatar paulrobertlloyd avatar rodrigoespinosa avatar rolodato avatar spaintrain avatar stefanneuhaus avatar thanosd avatar ukstv avatar xiaoxiangmoe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dotenv's Issues

Option to overwrite env variables

Hi,

I'm wondering why optional overwriting of existing env variables is not supported at the moment. Are there any problems with that feature?

It was implemented here: https://github.com/jordansexton/dotenv

My use case is as follows:

I want to set env variables in dev environment. I'm using gulp to restart project if some files change. In my case environment variables should change if I change .env file.

Should I prepare pull request with this feature?

Best Regards
Tomasz

require and Coffeescript

require('dotenv')() compiles to require('dotenv') in coffeescript, causing the following error:

dotenv.load();
       ^
TypeError: Object function dotenv() {
  dotenv = {
    version: package_json.version,
    environment: process.env.NODE_ENV || "development",

    _loadEnv: function() {
      return dotenv._setKeysAndValuesFromEnvFilePath(".env");
    },
    _loadEnvDotEnvironment: function() {
      return dotenv._setKeysAndValuesFromEnvFilePath(".env."+dotenv.environment);
    },
    _setKeysAndValuesFromEnvFilePath: function(filepath) {
      try {
        var data        = fs.readFileSync(filepath);
        var content     = data.toString().trim();
        var lines       = content.split('\n');

        for (var i=0; i<lines.length; i++) {
          var key_value_array = lines[i].split("=");
          var key             = key_value_array[0].trim();
          var value           = key_value_array[1].trim();

          if (value.charAt(0) === '"' && value.charAt(value.length-1) == '"') {
            value               = value.replace(/\\n/gm, "\n");
          }

          value               = value.replace(/['"]/gm, '');

          process.env[key]    = value;
        }
      } catch (e) {
      }

      return true;
    },
    load: function() {
      dotenv._loadEnvDotEnvironment();
      dotenv._loadEnv();

      return true;
    },
  };

  return dotenv;
} has no method 'load'

A quick workaround is to wrap it using backtick to use raw javascript. Couldn't you just remove the need for the trailing()` ?

Support for Arrays?

Hi, is their support for arrays? If so, what's the syntax like? Thank you.

.env.$NODE_ENV override no longer working after updating to > 1.x

main.js is mentioning "Allows configuration before loading .env and .env.$NODE_ENV".

I can't get per-environment config to work. Ex. starting my node app with NODE_ENV=test doesn't seems to load .env.test.

I'm trying to override some defaults in .env by using .env.test for some config that need to apply only when running tests ex. setting a different database when NODE_ENV is "test".

ability to use just parser

There have been quite a few discussions about changing the file name, to .whatever. I myself have even want that feature at some point for a CLI. I understand why this should not be added to the library, but how about making the library more flexible and allow access to additional methods.

var dotenv = require('dotenv');
var fs = require('fs');

fs.readFile('.custom', function( err, res ){
  if ( err ) return console.log('oops', err);
  var resp = dotenv.parse(res.toString('utf8'));
  console.log(resp); // { HELLO : 'world' }
});

right now there is some methods exposed but they return expected results

var resp = dotenv._getKeysAndValuesFromEnvFilePath('.custom');
console.log( resp ); // true;

Dotenv doesn't work when .env is missing?

For some reason the behavior of dotenv seems to have changed to require a .env file to exist, which breaks code in environments like Heroku. I'm getting the following when pushing a new Heroku app:

Error: Cannot find module 'dotenv'

Only way around it as far as I can tell it to wrap it in a conditional to only load on development.

Seems to me dotenv should silently fail if no .env exists. Thoughts?

calling config() actually loads() ??

I'm using dotenv as a sub-library. I want to configure it THEN load env variables at a later time (or maybe not calling it at all).

var dotenv = require('dotenv');
dotenv.config({ silent: true });

...later, maybe :
dotenv.load()

However, calling config() actually loads the .env file immediately. Looking at the dotenv code, I see :

module.exports.load = module.exports.config

This is completely unexpected and disregard the principle of last surprise.

Could the API be fixed ?

Can't configure port

Is it possible to configure PORT in a .env file? Tried for a while and gave up, is this a known issue?

Some variables are not recognized

Having the .env file (dev data, don't worry):

COOKIE_SECRET='$c|{u;%Z9|l8{=;ek"D1AhO?u}z,U*LWF<hp4]q_(!ClgH<4NKQ8BR`e:14PPKn%'

and using:

require('dotenv').load();

doesn't work, the variable is set, but to an empty string. But once I set it to be a simple string like:

COOKIE_SECRET=bbb

it works fine. [email protected].

Add load.js script for a cleaner invocation style in ES2015

Currently it's possible to load env vars with a single line using CommonJS:

require('dotenv').load();

But using ES2015 imports, it has to be two lines:

import dotenv from 'dotenv';
dotenv.load();

My suggestion is to add a script in the root of the project, load.js, which would do require('.').load();. This would make it possible to import and load in one neat statement:

import 'dotenv/load';

Importing dotenv in ES6

Support on ES6 has been tricky for me. Yesterday I tried preloading dotenv with iojs -r dotenv/config index.js. However, it didn't work and I couldn't understand why. My app did nothing. After running the app, a second or two later it would finish without doing anything.

I ended up with a very simple way to use dotenv. I simply import it like this:

import {} from 'dotenv/config'
import somethingElse from 'somethingElse'
...
[the rest of your code]

This works because of how ES6 modules imports modules. Before doing anything else in a file (say index.js) it does all the imports first. It does a depth first traversal of these imports, executing any code inside it. If we import dotenv first, it will execute config and add any env variables before doing anything else in the code.

From ES6 In Depth: Modules:

When you run a module containing an import declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.

So, importing dotenv on the first line of a bootstrap file in an app will set the env vars for anything that might use them.

I suggest we add a brief section about importing dotenv with ES6 modules to the README. But before sending a pull request and wanted to open it up for discussion here.

Any thoughts?

Document best practices

I struggle a long time to understand this 12-factors point about env-powered configurations.

I've read through all issues of this and the mirror Ruby repos. Through a number of stack overflow threads.

The final picture is still obscure to me. Maybe it's really a wrong direction (if it causes so much confusion and debates between people) or maybe everyone needs a better explanation of the entire workflow.

I surely see a number of benefits in the env approach. Cross-language / cross-platform way of app configuration being the most vivid. But I also see drawbacks and I'm yet to find some comparison table which would ensure me that benefits are bigger in the common case.

Here are the questions from the top of my head:

  1. Where to put default .env to copy at installation step? Should we exactly symlink or copy this file?

  2. If answer to 1) is copying: do you pass variable name to replace NODE_ENV value in destination .env at installation step?

  3. How exactly do you set different .env for testing? At what place it occurs?
    There are multiple ways to implement this. Which one is recommended?

  4. Does the lack of global fallback values cause practical issues (your personal experience)?
    process.env.xxx || <something> is more or less a DRY violation...
    Most config systems provide an option to set global fallback value for every config option.
    Did anyone ended implementing his own custom solution to cover this case?

  5. Does string-only config cause practical issues (your personal experience)?
    Most config systems use js files with native types (and highlighting!) available.
    Did anyone ended implementing his own custom solution to cover this case?

  6. Does the lack of a syntax for required config cause practical issues (your personal experience)?
    Most config systems provide this feature throwing exceptions in a "broken environment".
    Did anyone ended implementing his own custom solution to cover this case?

  7. Configuration in a frontend (client, browser)...

Personally, I find such blurry conventions undermine the whole "it's more obvious" promo declaration. It's hard to call something "superior" when it's just about tradeoffs and personal preferences.
I'd prefer to be wrong though.

[Added]: I already start to discover issues with 4) and 5). Most libraries expect boolean config values to be boolean. So we're going to repeat not only default values but also type conversions (or string comparisons) in every place it will be used...

port = parseInt(process.env.HTTP_PORT) || 80 // in every place
...
port = parseInt(process.env.HTTP_PORT) || 80 // in every place

useEtag = process.env.HTTP_USE_ETAG == "true" ? true || false // in every place
...
useEtag = process.env.HTTP_USE_ETAG == "true" ? true || false // in every place

๐Ÿ˜ž

dotenv crashes on commented lines

Given the following .env

BASIC=basic
SINGLE_QUOTES='single_quotes'
DOUBLE_QUOTES="double_quotes"
EXPAND_NEWLINES="expand\nnewlines"
DONT_EXPAND_NEWLINES_1=dontexpand\nnewlines
DONT_EXPAND_NEWLINES_2='dontexpand\nnewlines'
ENVIRONMENT_OVERRIDE=production
# COMMENTS=work
EQUAL_SIGNS=equals==

Anything after # COMMENTS in that file will be ignored. The problem is that the regex doesn't match so line 20 throws an error which is caught outside of the for loop in _getKeysAndValuesFromEnvFilePath and the loop stops. I should have a patch in a few.

optionally change the file name of ".env"

I have a complex set of environments which meant simultaneously having multiple .env.staging / .env.development / .env.development2 etc.

I would like to the ability to optionally override the file which you load env.

    _loadEnv: function(fileName) {
       if (!fileName) {
             fileName = ".env"
      }
      return dotenv._getKeysAndValuesFromEnvFilePath(fileName);
    },

Missing .env file throws error

So it looks like my last commit #11 breaks if there is no .env file present in the root directory. I'll create a pull request soon, but not exactly sure how to structure any unit tests given the project structure... suggestions?

Suppress errors of missing .env file for production

Production environments normally don't have a .env file. In these cases require('dotenv').load() prints the following error to console:

{ [Error: ENOENT, no such file or directory '.env'] errno: -2, code: 'ENOENT', path: '.env', syscall: 'open' }

Shouldn't these errors be suppressed or at least change into something less frightening?

Overrides

ATM the way overrides work seem backwards to me.

Any values set in .env trump those of the environment specific ones (e.g. .env.staging). Surely it should be the other way around, no? .env should be the default settings and the environment specific .env files should be the overriding ones. This way the environment specific env file values can be restricted to overriding the common, default values. In the current situation if for example there are 30 environment values and 20 of them are being overridden in some environment specific .env files you have to remove those 20 values from the default .env file and explicitly set them in all of the specific .env files. Meaning that if you decide that the default value of one of those environment values should be changed, you have to search through multiple environment specific .env files to change it.

I understand it should be possible to override any NODE_ENV values from the CLI, which is why I'd propose the following resolution mechanism:

CLI > .env.<specific> > .env

If you guys agree I'll issue a PR with the modifications and updated tests.

custom path doesn't work

Hi guys

I am trying to move my .env.production file into config dir.
However, it doens't work.

//var dotenv = require('dotenv'); // it works
var dotenv = require('dotenv').config({path: './config/.env.production'}); // doesn't work
dotenv.load();

config dir the same level as app.js

importing dotenv using ES6 imports

I figured out some trickiness that I had with es6 imports being transpiled with babel.

The issue is that if you have some code that looks like this...

import dotenv from 'dotenv';
dotenv.load();

import foo from 'foo'; // this file reads from process.env

babel will transpile it to something equivalent to

var dotenv = require('dotenv');
var foo = require('foo');
dotenv.load();

(note: the above is what the transpiled code would be functionally equivalent to.
If you want to see babels exact output you can look here)

As you can see, babel is moving up all the imports. This is because the es6 modules spec requires that all imports are loaded depth first before executing the module body.

The issue is that foo.js ends up getting required before dotenv.load() is called, so it won't process.env populated.

A solution (similiar to what was suggested in #89) is to change the code to look like

import 'dotenv/config';

import foo from 'foo'; // this file reads from process.env

What that does is import this file which essentially calls dotenv.config() as a side effect. This way by the time the 'foo' is imported dotenv has already loaded.

The only downside of this solution is that it will try to parse argv (which is not necessary). The way to work around that would be to add a load.js file in the root of the project that would just contain

require('./lib/main').load()

You would then be able to do something like this in es6

import 'dotenv/load';

import foo from 'foo'; // this file reads from process.env

Let me know if you're ok with adding a load.js and I'll open a PR to add the files and update the example.

Thanks! ๐Ÿ˜„

Setting NODE_ENV in .env doesn't work when using environment specific .env files

This:

environment:      process.env.NODE_ENV || "development",

in combination with this:

_loadEnvDotEnvironment: function() {
      return dotenv._getKeysAndValuesFromEnvFilePath(".env."+dotenv.environment);
}

is wrong in my opinion. If I have environment specific variables in my EnvDotEnvironment files and for example set NODE_ENV=production in my general .env file, it will still load .env.development instead of .env.production because it only sets environment once.

config Path

Hi is there a way to suppress loading of the cwd .env file when setting the path option within config?

Production server best practice documentation

So I feel it's very clear how to use dotenv locally, but what's the alternatives when deploying?
Should we just skip using dotenv, or should we use it with a limited set of env values that are commitable?
How does dotenv solve our production/test-server env variable setup?

Any advices? I couldn't find any docs on it.

composite variables don't work

seems like according to the docs that something like this
BASE_DIR=/var/www/modular_api
APP_SRC_DIR=${BASE_DIR}/src
APP_DIST_DIR=$BASE_DIR/dist/

should work however both both of the last 2 vars are blank ..

Ability to override .env via arguments

There are times when I would like to debug something real quick, and avoid having to open up my dotenv file.

Eg. I run this from terminal

DEBUG=app:DataAdapter node index

but I have a general debug statement in my detenv

DEBUG=server:IncomingRequest

Having the ability to not override things passed in as arguments might a good thing.

Example for multiline inconsistency

In the readme your explanation of multiline values says newlines are expanded in double quotes, but the example you give uses single quotes.

Use silent by default

Hey guys, I love the dotenv concept and use it in JS and Python. I'd like to suggest, though, that silent be true by default. I use .env files locally, but when I deploy somewhere like heroku, I don't (and can't) use them - I use actual environment variables via their config settings. It seems like a tool like dotenv should check if there is a .env file, and use that above all else, but gracefully fall back on actual environment variables if it isn't there. I can't think of a scenario where I'd want it to throw an error instead of looking for proper environment variables. And if there is a scenario, it certainly feels less common and thus should be explicitly set. Thoughts?

Adding on to variables

In the examples there are some basic variables. Im trying to create a ROOT variable and then add on to it like this: CLIENT_PATH=${ROOT}/path/to/client. Is this sort of thing supported?

windows?

I'm trying to use this to support users on windows (but don't have a windows box myself)
I got the feedback that creating a file just called .env doesn't work - win sees that as a file with no name, just an extension.

> i cannot remove -EXAMPLE and name it just .env

is there a recommended way to do this? perhaps just rename the file to dot.env ?
and require like this:

require('dotenv').config({path: 'dot.env'});

Should environment specific values override globals?

I haven't dug too deep into the ruby version, but based on the rails implementation shouldn't environment-specific environments override the global? ie., values in .env.development should override the same variables (if defined) that are in the main .env file.

Currently, the opposite is true. Changing this is just a matter of reversing the order of the calls in the main load() method.

Is this a bug, or am I expecting it to work backwards?

removing .trim() introduced whitespace into my database url

my .env file looks like this:

port=8007
ENVIRONMENT=dev
databaseUrl=user:[email protected]:port/db

My server looked like this:

var databaseUrl = process.env.databaseUrl;

I wasn't connecting and couldn't figure it out until I noticed a weird whitespace before the dbURL when console logging.

I now have to use this:

var databaseUrl = process.env.databaseUrl.trim();

ugh.

Typo in readme

The example of using a custom .env path should refer to the method _getKeysAndValuesFromEnvFilePath but it refers to _getKeyAndValueFromLine instead.

add 'replace' option to overwrite existing process.env keys

hi
the code ignores existing keys if already found in the process.env and does not overwrite them.
for some cases that works fine, however in some cases we keep a .env file which is expected to overwrite existing environment variables even if already present.
so I think an option would be helpful like that -
load({replace: true}) or load({force: true})
what do you think?
would love to submit a PR if that makes sense.
thanks!

Load .envrc files like direnv.

Either in addition to or as a fallback in case of missing .env file, it may be nice to also check for .envrc file, e.g. from direnv, and gracefully attempt to parse & load it.

Probably relies on an implementation of #112.

config() suggestions

  1. Add an option to config function, likes force.
    The force option is boolean variable, defaults to true, means that force override the process.env.
    If the force is false, just returns the {}.
  2. config(), returns {} or null

Not working after installing 1.0

.env

NODE_ENV=development
PG_URL=postgres://blah@localhost:5432/cool
.env.development

PG_URL=postgres://wtf:wtf@development:1234/development
0.5.1 > process.env.PG_URL === postgres://wtf:wtf@development:1234/development
1.0.0 > process.env.PG_URL === postgres://blah@localhost:5432/cool

if .env.${NODE_ENV} doesn't exist, there's no public way to load just .env

The logic I want is, let's say NODE_ENV is set to 'develop':

if '.env' exists 
   load .env and set environment 
else 
   fail

if '.env.develop' exists
    load it and set environment
else 
    log warning or some such

succeed

I realize I can do it by calling _loadEnv and _setEnv directly, but I'd rather use the "public" api. Maybe load needs to take some options to fine-tune behavior.

Boolean and null values

If there are environment variables set to null or false, they are cast as strings when saved to process.env

When they are read them into process.env there could be a check for boolean and null values, to ensure they are cast as such?

What do you reckon?

Document behaviour when environment variable is already set on the machine

My team had multiple issues between environments this week that, after much confusion, boiled down to environment variables not being set by dotenv because they were already set to different values on certain machines.

Of course we were being silly and this little detail of dotenv's behavior flew over our heads, but I think it would be extremely helpful to document this behavior as it is not mentioned anywhere and the (possibly erroneous) temptation is to assume that the environment of the application is whatever is in the .env file.

Also, maybe a boolean flag to specify which thing 'wins' would be very helpful, although not essential!

env override

Below my env files:

โ”œโ”€โ”€ .env
โ”œโ”€โ”€ .env.test

The .env is by default.
The .env.test will override the ENV.

First load .env then load .env.test.

I think that maybe need an option for override env.

https://github.com/motdotla/dotenv/blob/master/lib/main.js#L32-L34

Object.keys(parsedObj).forEach(function (key) {
        process.env[key] = override ? (parsedObj[key] || process.env[key]) : (process.env[key] || parsedObj[key])
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.