Node.js Reference ยท process
Node.js process.env
Reference for process.env in Node.js: read and set environment variables, why every value is a string, defaults, and safe handling of secrets.
process.env is an object that exposes the environment variables of the running process, such as PATH, HOME or NODE_ENV. It is the standard way to pass configuration and secrets into a program without writing them into the source code.
The object behaves like a dictionary of strings. Reading a variable that is not set gives undefined. Assigning a value converts it to a string, so setting a number or undefined does not do what you might expect. Changes affect only the current process and the child processes it starts afterwards.
const mode = process.env.NODE_ENV ?? 'development';
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
process.env: { [key: string]: string | undefined }
| Operation | Description |
|---|---|
process.env.NAME |
Reads a variable; undefined when it is not set. |
process.env.NAME = value |
Sets a variable. The value is converted with String() (deprecated for non-string values, but still works). |
delete process.env.NAME |
Removes a variable. |
'NAME' in process.env |
Tests whether it exists, even if the value is an empty string. |
Object.keys(process.env) |
Lists all variable names. |
Edge cases: on Windows, names are case-insensitive (process.env.Path and PATH are the same); on Linux and macOS they are case-sensitive. Assigning undefined stores the text "undefined".
Usage notes
- Everything is a string.
"false"and"0"are truthy. Parse booleans and numbers explicitly. - Delete, do not assign
undefined, to unset a variable. - Configuration, not state. Read variables once at startup, validate them, and pass a typed config object around instead of touching
process.envall over the code. - Keep secrets out of logs. Never print the whole object; it often contains tokens. Do not commit
.envfiles. - Loading a
.envfile: usenode --env-file=.env app.mjsorprocess.loadEnvFile(). Existing real variables normally win over the file. NODE_ENVis only a convention that libraries read (productionturns on optimizations). Node itself does not change behavior.- Child processes:
child_processpasses the current environment by default; pass an explicitenvoption to control it. - Setting
process.envinside a worker thread changes a copy that belongs to that worker unless you share the environment explicitly.