Course contentsShow
← JavaScript
Lesson 1510 of 2,72333. JavaScript in Node.jsFree lesson

Setting Environment Variables in Different Shells

Use shell-specific syntax to define environment variables before running Node scripts.

Setting Environment Variables in Different Shells

You'll learn how to set environment variables using the correct syntax for different shell environments before running your Node.js scripts.

The Challenge of Cross-Shell Syntax

While process.env in Node.js works the same way regardless of your operating system, setting environment variables before running a script depends entirely on which shell you're using. Windows Command Prompt, PowerShell, and Unix-like shells (bash, zsh) all use different syntax.

Shell-Specific Syntax

Unix-like Shells (bash, zsh, macOS Terminal)

NODE_ENV=production node app.js

You can set multiple variables in one line:

NODE_ENV=production PORT=8080 node app.js

Windows Command Prompt (cmd.exe)

set NODE_ENV=production && node app.js

Windows PowerShell

$env:NODE_ENV="production"; node app.js

Why This Matters

Think of environment variables like dialing in settings on different radio models—they all achieve the same result (playing music), but each has different buttons and knobs. Your Node.js code using process.env.NODE_ENV works identically everywhere, but configuring that variable requires knowing which "buttons" your shell uses.

Cross-Platform Solutions

For team projects, hardcoding shell-specific commands in documentation or scripts creates friction. This is why tools exist to abstract away these differences (though you'll learn about those solutions later).

Key Takeaway: Environment variables must be set using shell-specific syntax—VAR=value for Unix shells, set VAR=value && for cmd.exe, and $env:VAR="value"; for PowerShell—before running your Node script, even though process.env works identically inside Node.js.