env.dev

Node.js Environment Variables: process.env, dotenv & --env-file

How to use environment variables in Node.js. Covers process.env, dotenv, Node 20.6+ --env-file flag, NODE_ENV, type-safe validation with zod, and cross-platform support.

Last updated:

Loading…

Frequently Asked Questions

How do I read environment variables in Node.js?

Use process.env.KEY which returns the value as a string, or undefined if not set. All environment variable values are strings — you need to parse numbers, booleans, and JSON manually.

What is the --env-file flag in Node.js?

Node.js 20.6+ includes a built-in --env-file flag that loads .env files without any third-party package: node --env-file=.env app.js. It supports comments and basic syntax but not variable expansion.

What is NODE_ENV and why does it matter?

NODE_ENV is a convention (not a Node.js built-in) that indicates the runtime environment. Libraries like Express and React use it to toggle behavior: "production" enables optimizations, "development" enables verbose logging and dev tools.

Was this helpful?

Stay up to date

Get notified about new guides, tools, and cheatsheets.