config, dotenv, dotenv-safe, and env-cmd are all widely used npm packages for managing environment-specific configuration in Node.js applications. They help separate sensitive or environment-dependent settings — such as API keys, database URLs, or feature flags — from application code. config uses hierarchical JSON or YAML files merged by environment name, providing a structured and immutable configuration object. dotenv loads variables from a .env file into process.env with no validation. dotenv-safe extends dotenv by requiring all keys defined in a .env.example file to be present, adding a safety net. env-cmd loads .env files via the command line before script execution, keeping configuration logic outside the application code itself.
When building Node.js applications, managing environment-specific settings — like API keys, database URLs, or feature flags — is a foundational concern. The four packages under review (config, dotenv, dotenv-safe, and env-cmd) each tackle this problem differently, with distinct philosophies around source format, validation, runtime behavior, and deployment integration. Let’s examine how they work in practice and where each shines.
config uses hierarchical JSON (or YAML/JS) files organized by environment name.
config/default.json, config/production.json, etc.NODE_ENV.// config/default.json
{
"db": {
"host": "localhost",
"port": 5432
},
"apiTimeout": 5000
}
// config/production.json
{
"db": {
"host": "prod-db.example.com"
}
}
// In your app
const config = require('config');
console.log(config.get('db.host')); // 'prod-db.example.com' if NODE_ENV=production
dotenv, dotenv-safe, and env-cmd all rely on .env files (key=value format).
.env is loaded into process.env at startup.# .env
DB_HOST=localhost
DB_PORT=5432
API_TIMEOUT=5000
// With dotenv
require('dotenv').config();
console.log(process.env.DB_HOST); // 'localhost'
env-cmd loads .env files via CLI, not inside your code.
env-cmd directly.# package.json
{
"scripts": {
"start:dev": "env-cmd -f .env.development node server.js",
"start:prod": "env-cmd -f .env.production node server.js"
}
}
config provides schema-like validation through custom logic or external tools, but doesn’t enforce required keys out of the box.
const config = require('config');
if (!config.has('requiredKey')) {
throw new Error('Missing required configuration: requiredKey');
}
dotenv has no validation — it silently ignores missing or malformed entries.
.env, process.env.MY_KEY will be undefined.dotenv-safe enforces presence of all keys listed in a .env.example file.
.env against .env.example and throws if any expected variable is missing.# .env.example (defines required keys)
DB_HOST=
API_KEY=
// Throws if .env is missing DB_HOST or API_KEY
require('dotenv-safe').config();
env-cmd supports validation via --strict mode (as of v10+).
.env file has a non-empty value.env-cmd --strict -f .env node app.js
# Fails if any value in .env is empty
dotenv, dotenv-safe, and env-cmd all mutate process.env.
process.env.MY_VAR.// After dotenv loads
const port = process.env.PORT || 3000;
config provides an immutable, namespaced config object.
process.env directly for app settings.const config = require('config');
const port = config.get('server.port');
💡 Note:
configcan read fromprocess.envfor overrides (e.g.,CUSTOMER_DB_HOSToverridesconfig.customer.dbHost), but its primary interface is the config object.
env-cmd excels in script-driven environments like CI pipelines or Docker setups where you want to avoid bundling config logic into your app.
.env files per environment and don’t want conditional logic in code.dotenv and dotenv-safe are best when you ship .env files with your app (common in development or simple deployments).
.env at build time.config suits complex applications with layered configuration (e.g., SaaS platforms with tenant-specific overrides).
local.json), instance-specific configs, and runtime extensibility.None of these libraries support live config reloading out of the box.
config offers a util/watchedFile utility for manual file watching, but it’s not automatic.consul, etcd, or cloud-based parameter stores.You’re building a Next.js or Express app and need different DB URLs for dev and prod.
dotenv (for simplicity) or dotenv-safe (if you want safety)..env format, and sufficient for basic needs.// .env.development
DB_URL=postgresql://localhost/myapp_dev
// .env.production
DB_URL=$PROD_DB_URL // injected by CI
require('dotenv-safe').config({
allowEmptyValues: true,
example: '.env.example'
});
Your app has deep configuration trees (e.g., integrations.stripe.apiVersion, logging.transports.console.level).
config// config/default.json
{
"integrations": {
"stripe": {
"apiVersion": "2023-10-16",
"webhookSecret": "..."
}
}
}
const config = require('config');
const stripeConfig = config.get('integrations.stripe');
Your GitHub Actions workflow must fail fast if a required environment variable is missing.
dotenv-safe or env-cmd --strict# GitHub Actions
- name: Run tests
run: npx env-cmd --strict -f .env.test npm test
You deploy containers and inject config via mounted .env files or Kubernetes secrets.
env-cmd (if using file mounts) or dotenv (if copying .env into image)env-cmd keeps the app unaware of config mechanics.| Package | Config Format | Validation | Runtime Access | Best For |
|---|---|---|---|---|
config | JSON/YAML/JS | Manual/custom | config.get('x') | Complex apps, nested config, enterprise use |
dotenv | .env | None | process.env.X | Simple apps, quick setup, dev environments |
dotenv-safe | .env | Required keys (via .env.example) | process.env.X | Safety-critical apps, CI/CD pipelines |
env-cmd | .env | --strict mode | process.env.X | Script-driven deploys, Docker, no-code loading |
config.dotenv.dotenv-safe or env-cmd --strict.env-cmd.All four are actively maintained and safe for production use — the right choice depends entirely on your team’s workflow, deployment strategy, and tolerance for runtime risk.
Choose config if your application requires structured, hierarchical configuration with support for environment-specific overrides, nested objects, and programmatic access without mutating global state. It’s ideal for complex or enterprise-grade applications where configuration clarity, testability, and maintainability are critical.
Choose dotenv if you need a lightweight, zero-config way to load environment variables from a .env file during development or simple deployments. It’s perfect for small to medium projects where validation isn’t a priority and you’re comfortable reading directly from process.env.
Choose dotenv-safe when you want the simplicity of .env files but require strict enforcement that all expected environment variables are defined—typically to prevent runtime errors in CI/CD pipelines or production. It’s a drop-in replacement for dotenv with added safety.
Choose env-cmd if you prefer managing environment configuration entirely outside your application code—such as in npm scripts, Dockerfiles, or CI workflows—and want to avoid bundling config-loading logic into your runtime. It’s especially useful when deploying to containerized or serverless environments.
Node-config organizes hierarchical configurations for your app deployments.
It lets you define a set of default parameters, and extend them for different deployment environments (development, qa, staging, production, etc.).
Configurations are stored in configuration files within your application, and can be overridden and extended by environment variables, command line parameters, or external sources.
This gives your application a consistent configuration interface shared among a growing list of npm modules also using node-config.
The following examples are in JSON format, but configurations can be in other file formats.
Install in your app directory, and edit the default config file.
$ npm install config
$ mkdir config
$ vi config/default.json
{
// Customer module configs
"Customer": {
"dbConfig": {
"host": "localhost",
"port": 5984,
"dbName": "customers"
},
"credit": {
"initialLimit": 100,
// Set low for development
"initialDays": 1
}
}
}
Edit config overrides for production deployment:
$ vi config/production.json
{
"Customer": {
"dbConfig": {
"host": "prod-db-server"
},
"credit": {
"initialDays": 30
}
}
}
Use configs in your code:
const config = require('config');
//...
const dbConfig = config.get('Customer.dbConfig');
db.connect(dbConfig, ...);
if (config.has('optionalFeature.detail')) {
const detail = config.get('optionalFeature.detail');
//...
}
config.get() will throw an exception for undefined keys to help catch typos and missing values.
Use config.has() to test if a configuration value is defined.
Start your app server:
$ export NODE_ENV=production
$ node my-app.js
Running in this configuration, the port and dbName elements of dbConfig
will come from the default.json file, and the host element will
come from the production.json override file.
Type declarations are published under types/ and resolved via typesVersions. Subpath typings are included for config/async, config/defer, config/parser, config/raw, and config/lib/util in addition to the main config entrypoint.
If you still don't see what you are looking for, here are some more resources to check:
node-config contributors.May be freely distributed under the MIT license.
Copyright (c) 2010-2026 Loren West and other contributors