How to Install Node.js Express: A Step-by-Step Guide
Node.js Express is a minimal and flexible web application framework that offers a robust set of features for web and mobile applications. This tutorial will guide you through installing Node.js and setting up the Express framework on your system so you can start building modern web apps efficiently.
Prerequisites
- A computer running Windows, macOS, or Linux
- Basic command line knowledge
- Installed Node.js (Official site)
- A text editor like VS Code or your favorite IDE
- Internet connection to download packages
Step 1: Verify Node.js Installation
First, confirm Node.js is installed on your machine. Open your terminal and enter:
node -v
npm -v
You should see version numbers printed. If not, download and install Node.js from the official site linked above.
Step 2: Create a New Project Directory
Navigate to the directory where you want your project, then create a new folder:
mkdir my-express-app
cd my-express-app
This folder will hold your Express application files.
Step 3: Initialize a New Node.js Project
Initialize npm in your directory to create a package.json file which manages dependencies:
npm init -y
The -y flag uses default settings for simplicity.
Step 4: Install Express
Install the Express framework using npm:
npm install express
This installs Express locally to your project.
Step 5: Create the Express Server File
Create a file named app.js with the following basic server setup code:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World from Express!');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
This code starts a server that listens on port 3000 and responds with “Hello World from Express!” at the root URL.
Step 6: Run Your Express Application
Run your server with the command:
node app.js
You should see a console message confirming the server is running. Open your browser and visit http://localhost:3000 to see the greeting.
Troubleshooting Tips
- If you get “command not found” errors, ensure Node.js is installed and terminal is restarted
- For permission errors, try running npm commands with elevated privilege or use a Node version manager
- If port 3000 is in use, change
const port = 3000;inapp.jsto another port - Check your firewall settings if the server is not accessible on the network
Summary Checklist
- Verify Node.js and npm installed
- Create project directory and initialize npm
- Install Express package
- Create
app.jsfile with Express server code - Run
node app.jsand test in browser
For a more comprehensive understanding of server setup in Python, you might find our <a href="/
