Loads environment variables from .env file into process.env with support for encrypted .env.vault files
npx @tessl/cli install tessl/npm-dotenv@17.2.0Dotenv is a zero-dependency Node.js module that loads environment variables from .env files into process.env. It supports both standard .env files and encrypted .env.vault files for secure deployments, following The Twelve-Factor App methodology for configuration management.
npm install dotenvconst dotenv = require('dotenv');For ES6 modules:
import dotenv from 'dotenv';Auto-configuration (loads .env automatically):
require('dotenv/config');Or for ES6:
import 'dotenv/config';// Basic usage - loads .env from current directory
require('dotenv').config();
console.log(process.env.DATABASE_URL); // logs your env var
console.log(process.env.SECRET_KEY); // logs your env var
// With custom path
require('dotenv').config({ path: '/custom/path/to/.env' });
// With multiple paths (processed in order)
require('dotenv').config({ path: ['.env.local', '.env'] });
// Override existing environment variables
require('dotenv').config({ override: true });Dotenv is built around several key components:
config() function handles both standard and encrypted .env filesparse() function converts .env file contents to JavaScript objectspopulate() function merges parsed values into environment objects.env.vault files with DOTENV_KEYCore functionality for loading environment variables from .env files into process.env with support for encrypted vault files.
function config(options?: DotenvConfigOptions): DotenvConfigOutput;
function configDotenv(options?: DotenvConfigOptions): DotenvConfigOutput;Low-level parsing functionality for converting .env file contents into JavaScript objects without modifying process.env.
function parse<T extends DotenvParseOutput = DotenvParseOutput>(
src: string | Buffer
): T;Utility for merging parsed environment variables into target objects like process.env with flexible override options.
function populate(
processEnv: DotenvPopulateInput,
parsed: DotenvPopulateInput,
options?: DotenvPopulateOptions
): DotenvPopulateOutput;Support for encrypted .env.vault files using AES-256-GCM encryption for secure deployments.
function decrypt(encrypted: string, keyStr: string): string;Utility module that reads dotenv configuration from environment variables for auto-configuration scenarios.
// Exported as an object with configuration options parsed from environment variables
const envOptions: DotenvConfigOptions;Import:
const envOptions = require('dotenv/lib/env-options');Environment Variables:
DOTENV_CONFIG_ENCODING → options.encodingDOTENV_CONFIG_PATH → options.pathDOTENV_CONFIG_QUIET → options.quietDOTENV_CONFIG_DEBUG → options.debugDOTENV_CONFIG_OVERRIDE → options.overrideDOTENV_CONFIG_DOTENV_KEY → options.DOTENV_KEYUtility module that parses dotenv configuration from command-line arguments.
// Function that parses command-line arguments into configuration options
function parseCliOptions(args: string[]): DotenvConfigOptions;Import:
const parseCliOptions = require('dotenv/lib/cli-options');Usage:
// Parse command-line arguments
const options = parseCliOptions(process.argv);
// Supports: dotenv_config_encoding=utf8 dotenv_config_path=.env.localinterface DotenvConfigOptions {
path?: string | string[] | URL;
encoding?: string;
quiet?: boolean;
debug?: boolean;
override?: boolean;
processEnv?: DotenvPopulateInput;
DOTENV_KEY?: string;
}
interface DotenvConfigOutput {
error?: Error;
parsed?: DotenvParseOutput;
}
interface DotenvParseOutput {
[name: string]: string;
}
interface DotenvPopulateInput {
[name: string]: string;
}
interface DotenvPopulateOutput {
[name: string]: string;
}
interface DotenvPopulateOptions {
debug?: boolean;
override?: boolean;
}