In this section, you will learn how to install Node.js on Windows, macOS, and Linux, and how to set up a simple TypeScript-based Express project.
๐ Step 1: Node JS Install (Windows / macOS / Linux)
Before setting up a Node.js project, you need to install Node.js and npm (Node Package Manager).
โ For Windows (Recommended)
-
Go to the official Node.js website: https://nodejs.org
-
Download the LTS (Long-Term Support) version for Windows.
-
Run the installer:
-
Accept the license
-
Click “Next” on all default steps
-
Enable “Automatically install necessary tools” (optional, for advanced usage)
-
-
Once installed, open Command Prompt and verify installation:
node -v npm -v
If these commands return versions (e.g., v20.11.1
), Node.js is installed correctly.
๐ฅ๏ธ For macOS
Install Homebrew (if not already installed):
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
Then install Node.js:
brew install node
Verify the installation:
node -v npm -v
๐ง For Linux (Ubuntu/Debian)
sudo apt update sudo apt install nodejs npm -y
Or use NodeSource for latest versions:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash – sudo apt install -y nodejs
Verify installation:
node -v npm -v
๐ ๏ธ Step 2: Create Project Directory
โ ๏ธ All the following commands assume you’re using Command Prompt (Windows) or Terminal (macOS/Linux).
mkdir nodejs-project cd nodejs-project
๐ฆ Step 3: Initialize package.json
Create a package.json
file using:
nodejs-project > npm init
Follow the prompts or press Enter
to accept defaults. It will generate a file like this:
{ “name”: “nodejs-project”, “version”: “1.0.0”, “main”: “index.js”, “scripts”: { “test”: “echo \”Error: no test specified\” && exit 1″ }, “license”: “ISC” }
๐ What is the "scripts"
section?
“scripts”: { “test”: “echo \”Error: no test specified\” && exit 1″ }
The
scripts
section lets you define shortcut commands. For example, npm test
will run the command shown above.
๐งพ Step 4: Update package.json
Scripts
Open package.json
and replace the scripts
and main
fields like this:
“main”: “src/index.ts”, “scripts”: { “start”: “nodemon –exec ts-node src/index.ts” }
๐ Explanation:
-
start
: Runs your app usingnodemon
, which monitors for file changes. -
ts-node
: Executes TypeScript files directly without compiling to JS.
๐ฅ Step 5: Install Dependencies
Run this command to install all the required packages:
nodejs-project > npm install express nodejs-project >ย npm install –save-dev typescript ts-node nodemon @types/node @types/express
๐ Package Breakdown
Package | Description |
---|---|
express |
Web framework for handling routes and requests |
typescript |
Adds type safety to JavaScript |
ts-node |
Executes TypeScript files directly without compilation |
nodemon |
Automatically restarts server on file changes |
@types/node |
Type definitions for Node.js runtime |
@types/express |
Type definitions for Express.js |
๐ Step 6: Create Project Structure
Create a src
folder and an entry file:
nodejs-project > mkdir src nodejs-project > echo. > src\index.ts
Create a index.ts file
โ๏ธ Step 7: Add Express Server Code
Open src/index.ts
and paste the following code:
import * as express from ‘express’; const app: express.Application = express(); app.listen(5000, () => { console.log(‘Server is running at port 5000’); });
Step 8: Add TypeScript Configuration
Download the tsconfig.json
file and place it in the root of your project directory.
Download tsconfig.json
๐ Final Project Structure
nodejs-project/ โ โโโ node_modules/ # Installed dependencies โ โโโ src/ # Source code โ โโโ index.ts # Entry point of the application โ โโโ package.json # Project metadata and dependencies โโโ package-lock.json # Dependency lockfile โโโ tsconfig.json # TypeScript configuration
โถ๏ธ Step 9: Start the Server
Start your server using:
nodejs-project > npm start
You should see:
Server is running at port 5000
Any changes you make will automatically restart the server โ thanks to nodemon
.
โ Summary
In this lesson, you learned:
-
How to install Node.js on Windows, macOS, and Linux (๐ข Node JS Install)
-
How to set up a Node.js + TypeScript project
-
How to run and auto-restart a server using
nodemon
andts-node