Load Configuration Into the Enviroment in Node.js
Node.js·2 min read·Jan 13, 2026
When writing CLI tools, command-line flags are the best way to temporarily change the behavior of the program for a single execution, such as enabling verbose mode or selecting an output format.
However, CLI tools often also need a more persistent type of configuration and a safer way to store sensitive values, such as API keys, tokens, or secrets, without typing them on every command or leaking them into your shell history.
These values are usually stored in the shell environment, or in .env files whose contents are loaded and injected into the environment at startup.
📚 The shell environment, often abbreviated env, is a collection of variables in the form of name-value pairs.
For example:
HOME=/home/razvan/projectsPATH=/usr/bin:/bin:/usr/sbin:/sbin/USER=razvan
Access environment variables
In Node.js, the environment variables available in the shell session the script was launched from are exposed through the global process.env object:
process.env.ENV_VAR⚠️ While the
process.envobject can be modified by the script, such modifications won't be reflected outside of the Node.js process.
Unlock the program 🚀
Pay once, own it forever.
€79
30-day money-back guarantee
- 13 modules
- 113 lessons with full-code examples
- 29 projects with commented solutions
- All future lesson and project updates
- Lifetime access
By submitting this form, you agree to the Terms & Conditions and Privacy Policy.