dotenv

  • Version 16.4.5
  • Published
  • 79.1 kB
  • No dependencies
  • BSD-2-Clause license

Install

npm i dotenv
yarn add dotenv
pnpm add dotenv

Overview

Loads environment variables from .env file

Index

Functions

function config

config: (options?: DotenvConfigOptions) => DotenvConfigOutput;
  • Loads .env file contents into process.env by default. If DOTENV_KEY is present, it smartly attempts to load encrypted .env.vault file contents into process.env.

    See https://dotenvx.com/docs

    Parameter options

    additional options. example: { path: './custom/path', encoding: 'latin1', debug: true, override: false }

    Returns

    an object with a parsed key if successful or error key if an error occurred. example: { parsed: { KEY: 'value' } }

function configDotenv

configDotenv: (options?: DotenvConfigOptions) => DotenvConfigOutput;
  • Loads .env file contents into process.env.

    See https://dotenvx.com/docs

    Parameter options

    additional options. example: { path: './custom/path', encoding: 'latin1', debug: true, override: false }

    Returns

    an object with a parsed key if successful or error key if an error occurred. example: { parsed: { KEY: 'value' } }

function decrypt

decrypt: (encrypted: string, keyStr: string) => string;
  • Decrypt ciphertext

    See https://dotenvx.com/docs

    Parameter encrypted

    the encrypted ciphertext string

    Parameter keyStr

    the decryption key string

    Returns

    {string}

function parse

parse: <T extends DotenvParseOutput = DotenvParseOutput>(
src: string | Buffer
) => T;
  • Parses a string or buffer in the .env file format into an object.

    See https://dotenvx.com/docs

    Parameter src

    contents to be parsed. example: 'DB_HOST=localhost'

    Returns

    an object with keys and values based on src. example: { DB_HOST : 'localhost' }

function populate

populate: (
processEnv: DotenvPopulateInput,
parsed: DotenvPopulateInput,
options?: DotenvConfigOptions
) => void;
  • Loads source json contents into target like process.env.

    See https://dotenvx.com/docs

    Parameter processEnv

    the target JSON object. in most cases use process.env but you can also pass your own JSON object

    Parameter parsed

    the source JSON object

    Parameter options

    additional options. example: { debug: true, override: false }

    Returns

    {void}

Interfaces

interface DotenvConfigOptions

interface DotenvConfigOptions {}

    property debug

    debug?: boolean;
    • Default: false

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

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

    property DOTENV_KEY

    DOTENV_KEY?: string;
    • Default: undefined

      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.

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

    property encoding

    encoding?: string;
    • Default: utf8

      Specify the encoding of your file containing environment variables.

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

    property override

    override?: boolean;
    • Default: false

      Override any environment variables that have already been set on your machine with values from your .env file.

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

    property path

    path?: string | string[] | URL;
    • Default: path.resolve(process.cwd(), '.env')

      Specify a custom path if your file containing environment variables is located elsewhere. Can also be an array of strings, specifying multiple paths.

      example: require('dotenv').config({ path: '/custom/path/to/.env' }) example: require('dotenv').config({ path: ['/path/to/first.env', '/path/to/second.env'] })

    property processEnv

    processEnv?: DotenvPopulateInput;
    • Default: process.env

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

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

    interface DotenvConfigOutput

    interface DotenvConfigOutput {}

      property error

      error?: Error;

        property parsed

        parsed?: DotenvParseOutput;

          interface DotenvParseOutput

          interface DotenvParseOutput {}

            index signature

            [name: string]: string;

              interface DotenvPopulateInput

              interface DotenvPopulateInput {}

                index signature

                [name: string]: string;

                  interface DotenvPopulateOptions

                  interface DotenvPopulateOptions {}

                    property debug

                    debug?: boolean;
                    • Default: false

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

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

                    property override

                    override?: boolean;
                    • Default: false

                      Override any environment variables that have already been set on your machine with values from your .env file.

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

                    Package Files (1)

                    Dependencies (0)

                    No dependencies.

                    Dev Dependencies (10)

                    Peer Dependencies (0)

                    No peer dependencies.

                    Badge

                    To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                    You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/dotenv.

                    • Markdown
                      [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/dotenv)
                    • HTML
                      <a href="https://www.jsdocs.io/package/dotenv"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>