Install Express.js

🚀 Introduction

Express.js is a fast, lightweight, and flexible web framework for Node.js. Before building web applications or REST APIs with Express, you need to install Node.js, initialize a project, and add the Express package using npm.

Information

Express.js is distributed as an npm package and requires Node.js to be installed on your system.

📋 Prerequisites

  • Install Node.js (includes npm).
  • Use a code editor such as Visual Studio Code.
  • Basic familiarity with the command line.

🛠 Installation Steps

📁 Step 1: Create a Project Folder

Create Project Directory

mkdir express-app
cd express-app

This creates a new folder named express-app and navigates into it.

đŸ“Ļ Step 2: Initialize the Project

Initialize package.json

npm init -y

The command generates a package.json file that stores your project's metadata, scripts, and dependencies.

Tip

The -y option automatically accepts the default values, making project initialization quicker.

âŦ‡ī¸ Step 3: Install Express.js

Install Express

npm install express

This command downloads Express.js and its required dependencies into the node_modules directory and records it under the dependencies section of package.json.

📂 Project Structure After Installation

express-app
node_modules/
package.json
package-lock.json

📝 Step 4: Create Your First Express Application

index.js

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Welcome to Express.js!');
});

const PORT = 3000;

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

â–ļī¸ Step 5: Run the Application

Start the Server

node index.js

Open your browser and visit http://localhost:3000. You should see the message "Welcome to Express.js!".

Success

Congratulations! Your first Express.js server is now running successfully.

📄 Understanding package.json

FieldDescription
nameName of the project.
versionCurrent version of the application.
mainEntry point of the application.
scriptsCommands used to run the project.
dependenciesLists installed packages such as express.

📌 Verifying the Installation

Check Installed Packages

npm list express

This command displays the installed version of Express.js in your project.

⚡ Installing Express Globally (Optional)

In most cases, Express.js should be installed locally within each project. If you want to install the express-generator tool globally to quickly scaffold applications, use the following command:

Install Express Generator

npm install -g express-generator

Important

Install Express.js locally for each project. The global installation is intended only for the express-generator utility, not the Express framework itself.

💡 Best Practices

  • Initialize every project with package.json.
  • Install Express locally using npm install express.
  • Keep dependencies updated regularly.
  • Use version control to manage your project files.
  • Organize application code into folders such as routes, controllers, and middleware as the project grows.

📚 Learn More

Visit the official resources for more information:
â€ĸ Node.js Official Website
â€ĸ Express.js Official Documentation

📝 Summary

Summary

Installing Express.js involves creating a project directory, initializing it with npm, installing the Express package, creating a simple server, and running the application. Once installed, Express.js provides a powerful foundation for building modern web applications and RESTful APIs with Node.js.