Dotenv

Loads environment variables from .env for nodejs projects.

README

Dotenv is supported by the community.

Special thanks to:
Warp
Warp is a blazingly fast, Rust-based terminal reimagined to work like a modern app.
Get more done in the CLI with real text editing, block-based output, and AI command search.
Retool
Retool helps developers build custom internal software, like CRUD apps and admin panels, really fast.
Build UIs visually with flexible components, connect to any data source, and write business logic in JavaScript.
WorkOS
Your App, Enterprise Ready.
Add Single Sign-On, Multi-Factor Auth, and more, in minutes instead of months.



dotenv-vault

dotenv


dotenv

Dotenv is a zero-dependency module that loads environment variables from a .env file into [process.env](https://nodejs.org/docs/latest/api/process.html#process_process_env). Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.
BuildStatus Build status NPM version js-standard-style Coverage Status LICENSE Conventional Commits Featured on Openbase Limited Edition Tee Original Limited Edition Tee Redacted

Install


  1. ``` sh
  2. # install locally (recommended)
  3. npm install dotenv --save
  4. ```

Or installing with yarn? yarn add dotenv

Usage


Create a .env file in the root of your project:

  1. ```dosini
  2. S3_BUCKET="YOURS3BUCKET"
  3. SECRET_KEY="YOURSECRETKEYGOESHERE"
  4. ```

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

  1. ``` js
  2. require('dotenv').config()
  3. console.log(process.env) // remove this after you've confirmed it is working
  4. ```

.. or using ES6?

  1. ``` js
  2. import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
  3. dotenv.config()
  4. import express from 'express'
  5. ```

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

  1. ``` js
  2. require('dotenv').config()

  3. ...

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

Multiline values


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

  1. ```dosini
  2. PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
  3. ...
  4. Kh9NV...
  5. ...
  6. -----END RSA PRIVATE KEY-----"
  7. ```

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

  1. ```dosini
  2. PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n"
  3. ```

Comments


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

  1. ```dosini
  2. # This is a comment
  3. SECRET_KEY=YOURSECRETKEYGOESHERE # comment
  4. SECRET_HASH="something-with-a-#-hash"
  5. ```

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.

  1. ``` js
  2. const dotenv = require('dotenv')
  3. const buf = Buffer.from('BASIC=basic')
  4. const config = dotenv.parse(buf) // will return an object
  5. console.log(typeof config, config) // object { BASIC : 'basic' }
  6. ```

Preload


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.

  1. ``` sh
  2. $ node -r dotenv/config your_script.js
  3. ```

The configuration options below are supported as command line arguments in the format `dotenv_config_