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 our node and npm version:

node --version
npm --version

II. Create package.json

To add Express module in our project, we first need to 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 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.

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