Installation

This article is a step-by-step on how to install Express.

I. Install Node & Npm

To get started with the development using Express, we need to have Node.js and npm installed.

We can check our node and npm versions:

node --version
npm --version

II. Create package.json

To add the Express module to our project, we first create a project directory and then create a package.json file.

mkdir my-project
cd my-project
npm init -y

This code will generate a package.json file in the root of our project directory.

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

III. Install Express as a local dependency

Then, we install Express package as a dependency locally:

npm install express
{
  "name": "nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

IV. Install Postman

Postman is an application used for API testing. It can make GET, POST, DELETE, and other HTTP requests for testing.

You can download the app from their website.

V. Install Nodemon

Nodemon is a useful npm package that automatically restarts the node application when file changes in the directory are detected.

We can save nodemon as devDependencies.

npm i -D nodemon

Check the nodemon version:

nodemon --version

Then, in package.json file, we create a script for dev.

"scripts": {
    "start": "node index",
    "dev": "nodemon index"
  },

In this example, we have index.js as our main app file.

We can start our server when we run npm run dev in development. Nodemon will watch any changes and restart the server without having to do so.