I. What is npm?

If we want to use additional packages that is not available in Node core module, we can install them using npm, which is the Node package manager.

npm is automatically installed when we installed Node.js. We can use npm to install, update or remove Node packages on our computer and our projects.

The registries are where the packages (modules) are stored. Developers download packages from the npm registry and publish their packages to the registry.

Some essential npm packages:

  • nodemon: automatically restart the server whenever changes are made
  • express

II. Installing Node.js Modules with npm

There are two ways to install a module:

1. Globally

When we install a package globally, it means that we not only install it for our current project but we can also use it anywhere on our computer.

The packages will be installed in /usr/local.

npm install --global module-name
npm i -g module-name

The i is just an alias to install. There’s no difference between i and install.

Example: To install nodemon

npm install -g nodemon

To see the globally installed packages, run this command

npm ls --depth=0 -global

2. Locally:

We install most of our projects’ dependencies locally.

These modules go into the node_modules directory of local project:

npm install module-name
npm i module-name

To see the installed npm packages with their version, the command is

npm ls --depth=0

III. Updating packages

1. Updating local packages

In your project root directory, run the update command:

npm update

To test the update, run the outdated command. There should not be any output.

npm outdated

2. Updating global packages

Check which global packages need updating

To see which global packages need to be updated, run:

npm outdated -g --depth=0

Update a single global package

To update a single global package, on the command line, run:

npm update -g <package_name>

Updating all globally-installed packages

To update all global packages, on the command line, run:

npm update -g

IV. Package.json file

The package.json file keeps track of all packages we’ve installed locally to our project and meta data about the project as well, such as the descriptions, license, location, dependencies, scripts to build, launch and run.

We can configure many properties such as:

  • name
  • version number
  • dependencies
  • license
  • scripts

1. Creating package.json

To use 3rd-party packages in our project, we need to create package.json file.

To create a package.json file, run npm init command

$ npm init

This will initialize a package.json file.

It will ask us a series of questions about the project and it gives a default value in bracket. To accept them, we can press “Enter” or we can override it by typing a new one.

If you are okay with the default answers to these questions, you can skip the questions and answer yes to all of them automatically by using -y flag:

npm init -y
  • To see json documentation, run npm help json
  • To install a package and save it as a dependency in the package.json file, run:
npm install <pkg>

2. Example

For example, to install express:

  1. In a new project, create a package.json first by running npm init -y to initialize the project.
  2. In the project root folder (main folder), run: npm install express
  3. In a file where you want to use express, import it:
const express = require("express");
  • We can also use the npm install express@latest command line to upgrade to the latest module.

3. Listing and Removing Modules

To list what modules are installed, run npm ls from your root project location (where you have package.json and node_modules). It will display a tree of dependencies of this current project.

To list all globally installed modules, run: npm ls -g

To remove an npm module use the rm command: npm rm mysql

To remove a global module, apply the global flag: npm rm mysql -g

4. node_modules folder

When we’ve installed a package locally, it’s going to create a node_modules folder that contains all the files and packages that are needed for the package and dependencies of that package.

However, when people upload their projects on Github, they won’t include the folder because its size is large. So when we download that project, we may need to install that folder again.

We can do it by running this command:

npm install

This command will install all the dependencies that are listed in the package.json file.