From Zero to AI

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:

  1. The runtime to execute JavaScript code
  2. npm (Node Package Manager) to install tools like TypeScript
  3. 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

  1. Open your web browser
  2. Go to https://nodejs.org/
  3. Click the button for the LTS version (it will say something like "20.x.x LTS")
  4. The download will start automatically (a .msi file)

Step 2: Run the Installer

  1. Find the downloaded file (usually in your Downloads folder)
  2. Double-click the .msi file to start the installer
  3. Click "Next" to begin
  4. Accept the license agreement
  5. Keep the default installation location (usually C:\Program Files\nodejs\)
  6. On the "Custom Setup" screen, keep all defaults selected
  7. Important: Make sure "Add to PATH" is checked
  8. Click "Install"
  9. When prompted, allow the installer to make changes to your computer
  10. Click "Finish" when done

Step 3: Verify Installation

  1. Open Command Prompt (press Win + R, type cmd, press Enter)
  2. Type the following command and press Enter:
node --version

You should see something like:

v20.10.0
  1. 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

  1. Open Safari or your preferred browser
  2. Go to https://nodejs.org/
  3. Click the button for the LTS version
  4. Download the .pkg file

Step 2: Run the Installer

  1. Open the downloaded .pkg file
  2. 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
  3. 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

  1. Open Terminal (press Cmd + Space, type "Terminal", press Enter)
  2. Type the following command:
node --version

Expected output:

v20.10.0
  1. 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:

  1. Restart your computer - Sometimes PATH changes need a restart
  2. Reinstall Node.js - Make sure "Add to PATH" is checked during installation
  3. 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)

  1. Check if Node.js is installed:

    which node
    

    If this returns nothing, Node.js isn't installed or not in your PATH.

  2. For macOS with Homebrew, try:

    brew link node
    
  3. 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:

  1. Windows: Download the latest LTS from nodejs.org
  2. macOS: Use brew upgrade node or reinstall from nodejs.org
  3. 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:

  1. Open your terminal
  2. Run node --version - What version do you have? **____**
  3. Run npm --version - What version do you have? **____**
  4. Run node to enter the REPL
  5. Calculate 100 / 4 - What's the result? **____**
  6. Type .exit to 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 (node command) lets you run JavaScript interactively
  • Verify your installation with node --version and npm --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