Master Code
On The Go

Learn. Practice. Build.

How-To Guide

How to Install Express.js

Express is the most popular Node.js web framework. It's how you build APIs, web servers, and full-stack apps in JavaScript. Here's how to install it in 10 minutes.

📋 What You'll Need

✅ Express is free and open source.

🚂 What Is Express?

Express is a minimal Node.js framework that gives you:

It's the foundation of most Node backends and runs behind GitHub, Uber, and thousands of APIs.

1

Create a Project Folder

Open your terminal and run:

mkdir my-express-app
cd my-express-app
2

Initialize npm

This creates a package.json file — the manifest of your project:

npm init -y

The -y accepts all defaults. You can edit it later.

3

Install Express

npm install express

This downloads Express and adds it to your node_modules folder. It also adds it to package.json under dependencies.

💡 Using pnpm? pnpm add express does the same thing, faster.

4

Create Your First Server

Create a file called index.js and add:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello from Express!');
});

app.listen(3000, () => {
    console.log('Server running at http://localhost:3000');
});

Run it:

node index.js

Open http://localhost:3000 in your browser. You should see Hello from Express! 🎉

🧠 Essential Express Patterns

// Different HTTP methods
app.get('/users', handler);
app.post('/users', handler);
app.put('/users/:id', handler);
app.delete('/users/:id', handler);

// Return JSON
app.get('/api/data', (req, res) => {
    res.json({ message: 'Hello' });
});

// Read URL parameters
app.get('/users/:id', (req, res) => {
    res.send(`User ID: ${req.params.id}`);
});

// Read query strings (?name=Sarah)
app.get('/search', (req, res) => {
    res.send(`Searching: ${req.query.q}`);
});

// Parse JSON bodies
app.use(express.json());

app.post('/api/users', (req, res) => {
    console.log(req.body);
    res.json({ received: true });
});

🔧 Troubleshooting

"Cannot find module 'express'"

You didn't install Express, or you're in the wrong folder. Run npm install express in the same folder as index.js.

"Port 3000 already in use"

Another app is using that port. Change to app.listen(3001) or kill the other process.

Auto-restart on file change

Install nodemon: npm install -D nodemon, then run npx nodemon index.js. It restarts your server every time you save.

🎯 What's Next?

← All How-To Guides