Installing Node.js & Express
1. Install Node.js
Before using Express, you need to install Node.js.
Download & Install
- Go to Node.js official website and download the LTS version.
- Follow the installation instructions for your OS.
- Verify installation by running:
node -v npm -v
Using a Package Manager (Linux/macOS)
- Install via Homebrew (macOS):
brew install node
- Install via APT (Ubuntu/Debian):
sudo apt update sudo apt install nodejs npm
- Install via Homebrew (macOS):
Using Node Version Manager (Recommended)
- Install nvm:
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
- Install and use Node.js:
nvm install --lts nvm use --lts
- Install nvm:
2. Initialize a Node.js Project
Create a new project and navigate to its directory:
mkdir my-express-app
cd my-express-app
npm init -y # Creates package.json
3. Install Express
Install Express as a dependency:
npm install express
To install it globally (not recommended for projects):
npm install -g express
4. Verify Installation
Create a simple server in index.js
:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Run the server:
node index.js
Visit http://localhost:3000/
in your browser to confirm it works.