ExpressJS - Installation



In this chapter, we will learn how to start developing and using the Express Framework. To start with, you should have the Node and the npm (node package manager) installed. If you dont already have these, go to the ExpressJS Environment Setup chapter to install node on your local system. Confirm that node and npm are installed by running the following commands in your terminal.

node -v
v22.13.0

npm -v
10.9.2

Now that we have Node and npm set up, let us understand what npm is and how to use it.

Node Package Manager(npm)

npm is the package manager for node. The npm Registry is a public collection of packages of open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and countless other needs of the JavaScript community. npm allows us to access all these packages and install them locally. You can browse through the list of packages available on npm at npmJS.

How to use npm?

There are two ways to install a package using npm: globally and locally.

  • Globally − This method is generally used to install development tools and CLI based packages. To install a package globally, use the following code.

npm install -g <package-name>
  • Locally − This method is generally used to install frameworks and libraries. A locally installed package can be used only within the directory it is installed. To install a package locally, use the same command as above without the -g flag.

npm install <package-name>

Whenever we create a project using npm, we need to provide a package.json file, which has all the details about our project. npm makes it easy for us to set up this file. Let us set up our development project.

Step 1 − Start your terminal/cmd, create a new folder named hello-world and cd (create directory) into it −

E:\Dev>mkdir hello-world

E:\Dev>cd hello-world

E:\Dev\hello-world>

Step 2 − Now to create the package.json file using npm, use the following code.

E:\Dev\hello-world>npm init

It will ask you for the following information.

E:\Dev\hello-world>npm init

Just keep pressing enter, and enter your name at the author name field.

E:\Dev\hello-world>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install ` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (hello-world)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author: 'MAHESH PARASHAR'
license: (ISC)
About to write to E:\Dev\hello-world\package.json:

{
  "name": "hello-world",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "'MAHESH PARASHAR'",
  "license": "ISC",
  "description": ""
}


Is this OK? (yes)

npm notice
npm notice New major version of npm available! 10.9.2 -> 11.0.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.0.0
npm notice To update run: npm install -g npm@11.0.0
npm notice

E:\Dev\hello-world>

Step 3 − Now we have our package.json file set up, we will further install Express. To install Express and add it to our package.json file, use the following command −

E:\Dev\hello-world>npm install --save express

added 69 packages, and audited 70 packages in 4s

14 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Tip − The --save flag can be replaced by the -S flag. This flag ensures that Express is added as a dependency to our package.json file. This has an advantage, the next time we need to install all the dependencies of our project we can just run the command npm install and it will find the dependencies in this file and install them for us.

This is all we need to start development using the Express framework. To make our development process a lot easier, we will install a tool from npm, nodemon. This tool restarts our server as soon as we make a change in any of our files, otherwise we need to restart the server manually after each file modification. To install nodemon, use the following command −

E:\Dev\hello-world>npm install -g nodemon

added 29 packages in 2s

4 packages are looking for funding
  run `npm fund` for details

You can now start working on Express.

Advertisements