Lesson 5.1: Installing Node.js
Duration: 45 minutes
Learning Objectives
After completing this lesson, you will be able to:
- Understand what Node.js is and why developers use it
- Download and install Node.js on your operating system
- Verify that Node.js is installed correctly
- Understand what npm is and why it comes with Node.js
- Run a simple JavaScript command from the terminal
Introduction
Before you can write and run TypeScript code, you need a runtime environment. Think of it like this: you've learned about algorithms and how computers execute instructions, but now you need the actual software that makes it possible to run your code. That software is Node.js.
Node.js is the engine that powers modern JavaScript and TypeScript development. By the end of this lesson, you'll have it installed and running on your computer.
Main Content
What is Node.js?
┌─────────────────────────────────────────────────────────────┐
│ NODE.JS │
│ │
│ A runtime environment that allows you to run JavaScript │
│ (and TypeScript) outside of a web browser. │
│ │
│ ┌──────────────┐ ┌──────────────────┐ │
│ │ Your Code │ ──────────────────▶ │ Node.js Engine │ │
│ │ (.js, .ts) │ │ (executes code) │ │
│ └──────────────┘ └──────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Originally, JavaScript could only run inside web browsers like Chrome or Firefox. Node.js changed that by allowing JavaScript to run directly on your computer, just like Python or Java.
Why Do We Need Node.js?
| Without Node.js | With Node.js |
|---|---|
| JavaScript only works in browsers | JavaScript works everywhere |
| Can't build server applications | Can build servers, tools, apps |
| Limited to web pages | Full access to your computer |
| Can't run TypeScript | Provides the tools to compile TypeScript |
For our course, Node.js provides:
- The runtime to execute JavaScript code
- npm (Node Package Manager) to install tools like TypeScript
- A foundation for all modern JavaScript/TypeScript development
Choosing the Right Version
Node.js has two main release types:
┌─────────────────────────────────────────────────────────────┐
│ NODE.JS VERSIONS │
├──────────────────────────┬──────────────────────────────────┤
│ LTS │ Current │
│ (Long Term Support) │ (Latest Features) │
├──────────────────────────┼──────────────────────────────────┤
│ - Stable and tested │ - Newest features │
│ - Recommended for most │ - May have bugs │
│ - Updated for 30 months │ - Updated frequently │
│ - Best for learning │ - Best for experimentation │
└──────────────────────────┴──────────────────────────────────┘
👉 Choose LTS for this course
Always choose the LTS version unless you have a specific reason not to. It's more stable and better supported.
Installation Guide
For Windows
Step 1: Download the Installer
- Open your web browser
- Go to https://nodejs.org/
- Click the button for the LTS version (it will say something like "20.x.x LTS")
- The download will start automatically (a
.msifile)
Step 2: Run the Installer
- Find the downloaded file (usually in your Downloads folder)
- Double-click the
.msifile to start the installer - Click "Next" to begin
- Accept the license agreement
- Keep the default installation location (usually
C:\Program Files\nodejs\) - On the "Custom Setup" screen, keep all defaults selected
- Important: Make sure "Add to PATH" is checked
- Click "Install"
- When prompted, allow the installer to make changes to your computer
- Click "Finish" when done
Step 3: Verify Installation
- Open Command Prompt (press
Win + R, typecmd, press Enter) - Type the following command and press Enter:
node --version
You should see something like:
v20.10.0
- Also check npm:
npm --version
You should see something like:
10.2.3
For macOS
Option A: Using the Official Installer (Recommended for Beginners)
Step 1: Download the Installer
- Open Safari or your preferred browser
- Go to https://nodejs.org/
- Click the button for the LTS version
- Download the
.pkgfile
Step 2: Run the Installer
- Open the downloaded
.pkgfile - Follow the installation wizard:
- Click "Continue" through the introduction
- Accept the license agreement
- Choose the installation location (keep default)
- Click "Install"
- Enter your macOS password when prompted
- Click "Close" when installation completes
Option B: Using Homebrew (If you have it installed)
If you already have Homebrew installed, you can install Node.js with one command:
brew install node
Step 3: Verify Installation
- Open Terminal (press
Cmd + Space, type "Terminal", press Enter) - Type the following command:
node --version
Expected output:
v20.10.0
- Check npm:
npm --version
Expected output:
10.2.3
For Linux (Ubuntu/Debian)
Step 1: Update Your Package List
Open Terminal and run:
sudo apt update
Step 2: Install Node.js
For the latest LTS version, use NodeSource:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
Alternative: Using nvm (Node Version Manager)
This method gives you more control over Node.js versions:
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Restart terminal or run:
source ~/.bashrc
# Install latest LTS
nvm install --lts
Step 3: Verify Installation
node --version
npm --version
Understanding npm
When you install Node.js, you also get npm (Node Package Manager). Think of npm as an app store for code.
┌─────────────────────────────────────────────────────────────┐
│ NPM │
│ Node Package Manager │
│ │
│ ┌─────────────────────────────────────────────────────────┐│
│ │ npm Registry ││
│ │ (Over 2 million packages of reusable code) ││
│ │ ││
│ │ 📦 typescript 📦 react 📦 express ││
│ │ 📦 axios 📦 lodash 📦 jest ││
│ │ 📦 ...and millions more ││
│ └─────────────────────────────────────────────────────────┘│
│ │ │
│ ▼ │
│ npm install <package> │
│ │ │
│ ▼ │
│ Your Project Folder │
└─────────────────────────────────────────────────────────────┘
We'll use npm to:
- Install TypeScript
- Install helpful development tools
- Manage project dependencies
Your First Node.js Command
Let's make sure everything works by running some JavaScript directly in your terminal.
Step 1: Open the Node.js REPL
A REPL (Read-Eval-Print Loop) is an interactive environment where you can type code and see results immediately.
Open your terminal and type:
node
You should see:
Welcome to Node.js v20.10.0.
Type ".help" for more information.
>
Step 2: Try Some Commands
Type each of these and press Enter:
> 2 + 2
4
> "Hello, " + "World!"
'Hello, World!'
> console.log("Node.js is working!")
Node.js is working!
undefined
> Math.max(10, 5, 8, 12, 3)
12
Step 3: Exit the REPL
Press Ctrl + C twice, or type .exit and press Enter.
Congratulations! You've just run JavaScript code using Node.js!
Troubleshooting
"node is not recognized" (Windows)
This usually means Node.js isn't in your PATH. Try these solutions:
- Restart your computer - Sometimes PATH changes need a restart
- Reinstall Node.js - Make sure "Add to PATH" is checked during installation
- Manually add to PATH:
- Right-click "This PC" → Properties → Advanced system settings
- Click "Environment Variables"
- Under "System variables", find "Path" and click "Edit"
- Add
C:\Program Files\nodejs\ - Click OK and restart your terminal
"command not found: node" (macOS/Linux)
-
Check if Node.js is installed:
which nodeIf this returns nothing, Node.js isn't installed or not in your PATH.
-
For macOS with Homebrew, try:
brew link node -
For Linux, make sure you've sourced your profile:
source ~/.bashrc # or source ~/.zshrc
Permission Errors on npm (macOS/Linux)
If you see "EACCES" errors when using npm:
# Fix npm permissions
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Version is Too Old
If node --version shows a version older than 18:
- Windows: Download the latest LTS from nodejs.org
- macOS: Use
brew upgrade nodeor reinstall from nodejs.org - Linux: Use nvm to install a newer version:
nvm install --lts nvm use --lts
Practice Exercise
Exercise 1: Verify Your Installation
Complete these steps and write down your results:
- Open your terminal
- Run
node --version- What version do you have? **____** - Run
npm --version- What version do you have? **____** - Run
nodeto enter the REPL - Calculate
100 / 4- What's the result? **____** - Type
.exitto leave the REPL
Exercise 2: Explore the REPL
Open the Node.js REPL and try these commands:
// Try these one at a time
Date.now();
new Date().toLocaleDateString();
'typescript'.toUpperCase()[(1, 2, 3, 4, 5)].filter((n) => n > 2);
Exercise 3: Find Node.js Installation
Locate where Node.js is installed on your computer:
Windows:
where node
macOS/Linux:
which node
Write down the path: **____**
Key Takeaways
- Node.js is a runtime that lets you run JavaScript outside the browser
- Always install the LTS (Long Term Support) version for stability
- Node.js comes with npm, a package manager for installing tools and libraries
- The REPL (
nodecommand) lets you run JavaScript interactively - Verify your installation with
node --versionandnpm --version - Most installation problems are related to PATH configuration
Resources
| Resource | Type | Difficulty |
|---|---|---|
| Node.js Official Documentation | Documentation | Beginner |
| Node.js Download Page | Download | Beginner |
| Introduction to Node.js - nodejs.dev | Tutorial | Beginner |
| npm Documentation | Documentation | Beginner |
| nvm - Node Version Manager | Tool | Intermediate |