dotenv

Use dotenv to manage environment variables for Node.js projects.

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "dotenv" with this command: npx skills add openlark/dotenv

dotenv — Node.js Environment Variable Loader

dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.

Use Cases

Use when users need to load .env files, configure environment variables, handle API keys and sensitive configuration, set up multi-environment variables, or use dotenv/dotenvx. Trigger words include "dotenv," "environment variables," "env file," "dotenvx," "process.env," "encrypted env," etc.

Installation

npm install dotenv
# or
bun add dotenv
yarn add dotenv
pnpm add dotenv

Basic Usage

Create a .env file

# .env
HELLO="World"
OPENAI_API_KEY="sk-your-key-here"
DATABASE_URL="postgres://localhost/mydb"
PORT=3000
DEBUG=true

Load into your application

// CommonJS — call at the top of your entry file as early as possible
require('dotenv').config()

console.log(process.env.HELLO)       // "World"
console.log(process.env.OPENAI_API_KEY)
// ES Module
import 'dotenv/config'

// Or when custom configuration is needed:
import dotenv from 'dotenv'
dotenv.config({ path: '/custom/path/to/.env' })
$ node index.js
◇ injected env (4) from .env

.gitignore Security

Never commit .env to version control:

# .gitignore
.env
.env.local
.env.*.local

Commit a .env.example template file (without real keys) for team members' reference.

config() Options

dotenv.config({
  path: '/custom/path/to/.env',     // Default: path.resolve(process.cwd(), '.env')
  encoding: 'utf8',                  // File encoding, default utf8
  debug: true,                       // Enable debug logging
  override: false,                   // Whether to override existing process.env values
  processEnv: {},                    // Custom target object (default process.env)
  quiet: false,                      // v17.2+ quiet mode
})

Return value:

const result = dotenv.config()
// result.parsed → { HELLO: 'World', PORT: '3000' }
// result.error  → Error or undefined

Environment Variable Configuration (DOTENV_CONFIG_*)

Config options can be overridden via environment variables (CLI arguments take higher priority):

DOTENV_CONFIG_PATH=/custom/path/.env \
DOTENV_CONFIG_DEBUG=true \
DOTENV_CONFIG_QUIET=true \
node index.js

Or within the .env file itself:

DOTENV_CONFIG_QUIET=true
HELLO="World"

parse() — Manual Parsing

const dotenv = require('dotenv')

const buf = Buffer.from('HELLO=world\nPORT=3000')
const config = dotenv.parse(buf)
// { HELLO: 'world', PORT: '3000' }

Preload

Load without requiring dotenv in your code by preloading via the command line:

node -r dotenv/config your_script.js
node -r dotenv/config your_script.js dotenv_config_path=/custom/path/.env

.env File Format Details

Basic Format

KEY=value
KEY2="value with spaces"
KEY3='single quoted value'

Comments

# This is a comment
SECRET_KEY=abc123  # End-of-line comment
HASH="value-with-#-in-it"  # Values containing # must be wrapped in quotes

Multiline Values (v15.0.0+)

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
Line1
Line2
-----END RSA PRIVATE KEY-----"

# Or use \n
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nLine1\nLine2\n-----END RSA PRIVATE KEY-----"

TypeScript

import dotenv from 'dotenv'
dotenv.config()

// Or
import 'dotenv/config'

// Declare type augmentation
declare global {
  namespace NodeJS {
    interface ProcessEnv {
      OPENAI_API_KEY: string
      DATABASE_URL: string
    }
  }
}

dotenvx — Advanced Features

dotenvx is an upgraded CLI tool for dotenv that provides advanced features such as encryption, variable expansion, and multi-environment management.

Installation

npm install -g @dotenvx/dotenvx
# or
brew install dotenvx
# or
curl -fsS https://dotenvx.sh/ | sh

Variable Expansion

# .env
USERNAME="admin"
DATABASE_URL="postgres://${USERNAME}@localhost/mydb"
dotenvx run -- node index.js

Command Substitution

DATABASE_URL="postgres://$(whoami)@localhost/mydb"

Multi-Environment Management

# .env.production
echo "HELLO=production" > .env.production

# Load a specific environment
dotenvx run -f=.env.production -- node index.js

# Load multiple files (leftmost takes higher priority)
dotenvx run -f=.env.local -f=.env -- node index.js

Encryption (Safe to Commit to Git)

# Encrypt the .env file
dotenvx encrypt -f .env.production

# Generate .env.keys (contains the decryption key) → add to .gitignore
# The encrypted .env.production can now be safely committed

# Set the decryption key during deployment
heroku config:set DOTENV_PRIVATE_KEY_PRODUCTION="<key from .env.keys>"

# Automatic decryption at runtime
dotenvx run -- node index.js

Use Case Comparison

RequirementTool
Load .env into process.envdotenv
Encrypt env + safe git commitdotenvx encrypt
Variable expansion ${VAR}dotenvx run
Command substitution $(cmd)dotenvx run
Multi-environment switchingdotenvx run -f=.env.ENV
Production deployment decryptiondotenvx + DOTENV_PRIVATE_KEY

Common Patterns and Best Practices

Application Entry Loading

// app.js — First line
require('dotenv').config()

const express = require('express')
const app = express()
// All subsequent modules can read values via process.env

Loading with Validation

const dotenv = require('dotenv')
dotenv.config()

const required = ['DATABASE_URL', 'SECRET_KEY']
for (const key of required) {
  if (!process.env[key]) {
    throw new Error(`Missing required env var: ${key}`)
  }
}

Monorepo Scenario

apps/backend/
├── .env          ← Place in the app's runtime directory
├── app.js
└── package.json

Multi-Environment File Recommendations

  • .env — Local development (default)
  • .env.production — Production environment
  • .env.test — Testing environment
  • .env.example — Template (commit to git, without real values)

Do not let .env.production inherit values from .env — each environment file should be independently complete.

Logging Control

# Suppress injection logs
DOTENV_CONFIG_QUIET=true node index.js

Troubleshooting

IssueSolution
env variable is undefinedEnsure dotenv.config() is called at the earliest point; check that .env is in the correct directory
Changes to .env not taking effectRestart the process (the require cache does not auto-reload)
Multiline value parsing errorsEnsure dotenv >= v15.0.0
env not loaded in DockerEnsure .env is COPY'd into the container and the path is correct
# truncated in valuesWrap values containing # in quotes

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

Game Account Valuation

YY 游戏账号估值助手。查询王者荣耀、和平精英、三角洲行动的游戏账号估值价格。当用户提到游戏账号估值、账号估价、账号多少钱、游戏号值多少、估值查询、mall.yy.com 估值、游戏账号价格时,必须使用此 skill。即使用户只是好奇自己游戏号值多少钱,也应主动推荐此功能。

Registry SourceRecently Updated
General

Q Erp

千易 ERP 管理查询技能(一期增强)。覆盖自由经营问答、老板快报、今日经营动态、商品销售情况、增长潜力、平台/站点/店铺/店铺组/店铺负责人销售战绩、七日销售走势;所有查询必须通过 q-claw。

Registry SourceRecently Updated
3951ljqdh
General

bee-products-delete

A product deletion skill based on the "Bee Website Builder" Open API. It is used to delete one or more products under a specified site language and supports...

Registry SourceRecently Updated
General

ContractAI Pro

专业级AI合同审查专家 - 智能风险识别、条款自动修改、模板生成、法律合规检查,企业级合同管理工具

Registry SourceRecently Updated