Lesson 10/100

Tutorials Node.js Tutorial

Environment Variables — Complete Guide

Environment Variables — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Node.js Tutorial on Toolliyo Academy.

On this page
Environment Variables
Lesson 10 of 100 · Module 1: Node.js Foundations · BEGINNER
Topic: Environment Variables · Level: BEGINNER · Read time: ~12 min + hands-on

Environment Variables

This lesson covers Environment Variables. Think of this lesson as a short workshop you can run on your laptop.

What you will learn

  • What environment variables means — in normal words, not textbook words
  • How it works step by step
  • Code you can run today on your laptop
  • Where teams use this in real projects

Before you start

Explain it simply

Environment variables store settings outside your code — database URLs, API keys, port numbers.

Think of it like this: A restaurant kitchen: one chef (main thread) takes orders, while helpers (libuv) handle oven timers and deliveries without blocking the chef.

Why developers use this

  • Keep secrets out of git
  • Change config per machine without editing code
  • Standard practice in Docker and cloud

How it works (step by step)

  1. You write JavaScript in a .js file about Environment Variables.
  2. You run it with node filename.js in the terminal.
  3. Node prints output or starts a server depending on the lesson.
  4. You change one line, run again, and see what changed — that is how you learn.

Code example — type this yourself

require('dotenv').config();
const port = process.env.PORT || 3000;
console.log('Listening on', port);

Create a .env file with PORT=4000. dotenv loads it into process.env. Never commit .env to git.

What each part does

  • require('dotenv').config(); — Loads a built-in module or package you installed with npm.
  • const port = process.env.PORT || 3000; — Reads config from environment variables — safe place for secrets.
  • console.log('Listening on', port); — Prints to the terminal — great for learning; use proper logging in production.

Real life: where Environment Variables shows up

A startup team uses Environment Variables when they bootstrap their first API. The developer runs a small script on a laptop, stores config in .env, and splits code into modules before the app grows. Start small: one feature working beats a perfect architecture on paper.

Try it yourself — hands-on

  1. npm install dotenv
  2. Create .env with PORT=4000
  3. Run your script and confirm the port logs
Tip: In production, the host (Railway, Azure) sets env vars — you do not upload .env.

Common mistakes (avoid these)

  • Pushing API keys to GitHub — rotate them immediately if that happens.

Interview note

Interviewers often ask: “What is Environment Variables?” Answer in one sentence, then give a tiny example you actually ran.

Summary

  • Store secrets in .env, not in source code
  • Use process.env.PORT with a default
  • Add .env to .gitignore

Continue learning

Previous: Process Object — Complete Guide

Next: Event Loop — Complete Guide

Lesson 10 of 100 · Node.js Tutorial

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Node.js Tutorial
Course syllabus

Node.js Tutorial

Module 1: Node.js Foundations
Module 2: Async Programming
Module 3: Express.js & EJS
Module 4: REST APIs & Databases
Module 5: Real-Time & Event Systems
Module 6: Advanced Node.js
Module 7: Performance & Security
Module 8: Testing & Deployment
Module 9: Latest Node.js Features
Module 10: Enterprise Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details