Installing Express.js
Express.js is a web framework for Node.js that makes it easier to create web applications and APIs. Before using Express.js in a project, we need to create a Node.js project and install the Express package.
In this lesson, we will learn how to check Node.js and npm, create a project folder, initialize the project, install Express.js, verify the installation, and run a basic Express application.
1. What Is Express.js Installation?
Installing Express.js means adding the Express package to a Node.js project so that we can use its routing, request-handling, and response-handling features.
For example, Samadhan wants to create a course information API for CIIT Training Institute. First, he installs Node.js, creates a project, and then installs Express.js to build the API.
npm install express
This command installs Express.js and records it as a dependency
in the project's package.json file.
2. Prerequisites for Installing Express.js
Before installing Express.js, make sure Node.js and npm are available on your computer.
| Requirement | Purpose | Verification Command |
|---|---|---|
| Node.js | Runs JavaScript outside the browser. | node -v |
| npm | Installs and manages project packages. | npm -v |
| Code Editor | Used to create and edit project files. | Visual Studio Code or another editor |
Step 1: Check the Node.js version.
node -v
Example output:
v22.0.0
The displayed version depends on the version installed on your computer. The output above is only an example.
Step 2: Check the npm version.
npm -v
Example output:
10.8.0
If these commands display version numbers, Node.js and npm are available in your terminal. If a command is not recognized, check the installation and PATH configuration.
3. Creating a Node.js Project
Before installing Express.js, create a separate folder for the project. This keeps the source code and dependencies organized.
Step 1: Create a project folder.
mkdir express-demo
The mkdir command creates a folder named
express-demo.
Step 2: Open the project folder.
cd express-demo
The cd command changes the current working directory
to the project folder.
4. Project Setup Flow
The following diagram shows the steps involved in preparing a project before installing Express.js.
Verify the development environment
mkdir express-demo
cd express-demo
npm init -y
5. Initializing the Project with npm
The npm init command initializes a Node.js project
by creating a package.json file. This file stores
project metadata, scripts, and dependency information.
Command:
npm init -y
The -y option accepts the default answers to the
initialization questions.
Example package.json:
{
"name": "express-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
The generated values may differ depending on the folder name
and npm version. The important point is that the project now
has a package.json file.
6. Installing Express.js
Once the project is initialized, install Express.js using npm.
npm install express
npm downloads Express.js and the required dependencies, places
them in the node_modules folder, and updates the
project dependency information.
After installation, the project structure looks like this:
express-demo/
|
|-- node_modules/
|
|-- package.json
|
|-- package-lock.json
|
| File or Folder | Purpose |
|---|---|
node_modules |
Contains installed packages and their dependencies. |
package.json |
Contains project information and declared dependencies. |
package-lock.json |
Records the resolved dependency versions for reproducible installs. |
7. Verifying the Express.js Installation
After installation, we can check whether Express.js appears in the project's dependency tree.
npm list express
Example output:
express-demo@1.0.0
`-- express@4.x.x
The version shown in your terminal may be different. A successful installation means the package is available to the project.
You can also inspect package.json and look for
Express.js under dependencies.
8. Creating Your First Express Server
Now that Express.js is installed, create a file named
server.js inside the project folder.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Express.js installed successfully!');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
Explanation:
| Code | Explanation |
|---|---|
require('express') |
Imports the installed Express.js package. |
express() |
Creates an Express application. |
app.get('/', ...) |
Defines a GET route for the home page. |
res.send() |
Sends a response to the client. |
app.listen() |
Starts the server on the specified port. |
9. How the Installed Express Server Works
When the application starts, it listens for incoming requests. If the client opens the home page, Express.js matches the route and returns the configured message.
node server.js
Waits for incoming HTTP requests
GET /
res.send()
Express.js installed successfully!
10. Running and Testing the Application
Open a terminal in the folder containing server.js
and execute the following command.
node server.js
Expected terminal output:
Server running at http://localhost:3000
Open the following URL in your browser:
http://localhost:3000/
Expected browser output:
Express.js installed successfully!
If the message appears in the browser, Express.js is installed and your first Express server is working.
11. Common Installation Problems
| Problem | Possible Reason | What to Check |
|---|---|---|
node is not recognized |
Node.js is not installed or PATH is not configured. | Install Node.js and reopen the terminal. |
npm is not recognized |
npm is unavailable in the terminal. | Check the Node.js installation and PATH. |
Cannot find module 'express' |
Express is not available to the running project. | Run npm install express in the project folder. |
EADDRINUSE |
The port is already in use. | Stop the other server or use another available port. |
12. Practice Tasks
Task 1: Install Express.js
- Check Node.js and npm versions.
- Create a folder named
my-express-app. - Initialize the project using
npm init -y. - Install Express.js using
npm install express. - Verify the installation using
npm list express.
Expected result: Express.js should appear in the project's installed dependencies.
Task 2: Run an Express Server
- Create a file named
server.js. - Import Express.js and create an application.
- Define a GET route for
/. - Return a welcome message using
res.send(). - Start the server and test it in the browser.
Expected result: The browser should display the welcome message returned by the Express application.
Summary
In this lesson, we learned how to prepare a Node.js project, initialize it using npm, install Express.js, and verify the installation. We also created and tested a basic Express server using a GET route.
Remember the flow: Check Node.js and npm → Create Project → Initialize with npm → Install Express → Create Server → Test in Browser.