Node.js YAML Config to .env with dotenv

Convert Node.js YAML configuration files to .env format compatible with the dotenv package. Learn key naming patterns and type handling for JavaScript runtimes.

Real-World Configs

Detailed Explanation

Node.js applications commonly use either YAML config files (with packages like js-yaml or config) or .env files (with dotenv). Converting between them requires understanding how each approach handles types, naming, and structure.

Node.js config.yml (used with node-config):

app:
  name: MyAPI
  port: 3000
  host: 0.0.0.0
  cors:
    enabled: true
    origins:
      - http://localhost:3000
      - https://myapp.com
database:
  client: pg
  connection:
    host: localhost
    port: 5432
    database: myapp_dev
    user: postgres
    password: localpass
  pool:
    min: 2
    max: 10
auth:
  jwt_secret: dev-secret-change-me
  token_expiry: 3600
  refresh_expiry: 86400
logging:
  level: debug
  format: json
  file: /var/log/app/combined.log

Equivalent .env file (for dotenv):

# Application
APP_NAME=MyAPI
APP_PORT=3000
APP_HOST=0.0.0.0
APP_CORS_ENABLED=true
APP_CORS_ORIGINS=http://localhost:3000,https://myapp.com

# Database
DATABASE_CLIENT=pg
DATABASE_CONNECTION_HOST=localhost
DATABASE_CONNECTION_PORT=5432
DATABASE_CONNECTION_DATABASE=myapp_dev
DATABASE_CONNECTION_USER=postgres
DATABASE_CONNECTION_PASSWORD=localpass
DATABASE_POOL_MIN=2
DATABASE_POOL_MAX=10

# Auth
AUTH_JWT_SECRET=dev-secret-change-me
AUTH_TOKEN_EXPIRY=3600
AUTH_REFRESH_EXPIRY=86400

# Logging
LOGGING_LEVEL=debug
LOGGING_FORMAT=json
LOGGING_FILE=/var/log/app/combined.log

Reading the .env in Node.js:

require('dotenv').config();

const config = {
  app: {
    name: process.env.APP_NAME,
    port: parseInt(process.env.APP_PORT, 10),
    host: process.env.APP_HOST,
    cors: {
      enabled: process.env.APP_CORS_ENABLED === 'true',
      origins: process.env.APP_CORS_ORIGINS?.split(',') || [],
    },
  },
  database: {
    client: process.env.DATABASE_CLIENT,
    connection: {
      host: process.env.DATABASE_CONNECTION_HOST,
      port: parseInt(process.env.DATABASE_CONNECTION_PORT, 10),
      // ...
    },
  },
};

Key differences in the Node.js ecosystem:

  1. Type parsing is manual. YAML automatically types values (numbers, booleans). With dotenv, process.env values are always strings -- you must explicitly call parseInt(), compare with === 'true', etc.

  2. Arrays become comma-separated strings. The YAML array origins: [a, b] must be stored as ORIGINS=a,b and split in code with .split(',').

  3. No nested structure. YAML's tree structure requires flattening to flat ENV keys. The consuming code must reconstruct the object shape.

  4. dotenv does not support variable expansion by default. Use dotenv-expand for ${VAR} syntax.

Recommended approach: Use YAML for local development config and .env for deployment secrets. Tools like config support both and can merge them with environment variables taking precedence.

Use Case

Converting a Node.js application from yaml-config-based settings to environment variables for deployment on a serverless platform (Vercel, Netlify, AWS Lambda) where YAML config files cannot be bundled.

Try It — YAML ↔ ENV Converter

Open full tool