Skip to main content

How to Read an Environment Variable in Node.js

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that change between computers, such as a port, a database address or an API key, belong in environment variables rather than in your code. Node.js gives you all of them in one object, process.env.

Syntax​

Syntax
const <value> = process.env.<NAME>;
PartWhat it does
process.envAn object holding every environment variable the program started with.
<NAME>The variable's name. On macOS and Linux it must match exactly, capitals included.
<value>The variable's value as a string, or undefined when it isn't set.

The Node.js documentation describes it in one line: "The process.env property returns an object containing the user environment."

The Example​

The example prints the value of a variable named GREETING, first with the variable set and then without it.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which reads process.env.GREETING into a constant and prints GREETING is: followed by its value.

main.js
const greeting = process.env.GREETING;

console.log(`GREETING is: ${greeting}`);

Step 2: Set the Variable and Run It​

Set GREETING to Hello and run the program. Then remove the variable and run it again:

Terminal
export GREETING=Hello
node main.js
unset GREETING
node main.js

A zsh terminal where export GREETING=Hello and node main.js print GREETING is: Hello, and after unset GREETING the same command prints GREETING is: undefined.

Output
GREETING is: Hello
GREETING is: undefined

With the variable set, process.env.GREETING held 'Hello'. Once it was removed, the same line gave undefined: reading a missing variable isn't an error, so check for it when your program needs it.

Values Are Always Strings​

Every value in process.env is a string, even when it looks like a number. Started as PORT=3000 node main.js, typeof process.env.PORT is 'string', so convert it with Number(process.env.PORT) before doing arithmetic.

Common Mistakes​

MistakeWhat happensDo this instead
Setting the variable in one terminal and running Node.js in anotherundefined: each terminal has its own variablesSet it in the terminal that runs the program
Using a number from process.env directly'3000' + 1 is '30001'Convert it first with Number(...)
Changing the case, as in process.env.greeting on macOS or LinuxundefinedUse the exact name

Troubleshooting​

SymptomLikely causeFix
The value is undefinedThe variable isn't set in this terminalSet it as in Step 2, then run again
node: missing.env: not found--env-file points at a file that doesn't existFix the path, or use --env-file-if-exists

FAQ​

How do I load variables from a .env file?​

Start Node.js with node --env-file=.env main.js. It reads lines such as GREETING=Hello from .env into process.env. With --env-file-if-exists, a missing file prints missing.env not found. Continuing without it. instead of stopping.

Are variable names case-sensitive?​

On macOS and Linux, yes. The Node.js documentation notes that "On Windows operating systems, environment variables are case-insensitive."

Conclusion​

process.env.NAME reads an environment variable as a string, and gives undefined when it isn't set. Set variables with export in zsh or bash and $env: in PowerShell, or load them from a file with --env-file.

Additional Resources​