How to Install NPM Packages (also Update and Uninstall)

In this article, we will cover:

  • How to install NPM packages from the command line with the most important parameters.
  • How to find outdated NPM packages
  • How to update NPM packages
  • How to uninstall NPM packages

How to Install NPM packages

First, fire up any terminal. I use the integrated terminal from VS Code.

You need to make sure you have Node.js and NPM installed.

In your command line, write these commands:

node -v
npm -v

You should get a version number when entering the commands above.

Create a package.json file

Before you start installing packages you need a package.json file. This file keeps track of all the packages you use in your projects.

To create a package.json file, start by creating a new folder and in this folder write the following in your command line:

npm init -y

The -y parameter will just automatically answer the default values to things like name, version, etc.

Your package.json file should look something like this:

  "name": "npm-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": [],
  "author": "",
  "license": "ISC"

Let’s say we are creating an express server. To install the express package, we will write the following:

npm install express

If you look at you package.json file, you can see that it added a dependencies section with the packages in your project:

"dependencies": {
  "express": "^4.17.1"

You can also install packages by using this shorthand command:

npm i express

What does –save means?

If you look at some tutorials, you will notice that sometimes packages are installed like this:

npm install express --save

You had to include the --save option on NPM versions prior to 5.0.0. For newer versions, this still works, but it is obsolete.

How to Install Packages Globally

Until now, we have only installed a package on our current project. We can also install packages globally.

We usually install global packages for tools that aren’t directly related to our project.

For instance, a tool like Nodemon is something you could install globally. Nodemon is a tool that watches your files and automatically refreshes when files in your Node.js app are saved.

Install NPM package globally:

npm install nodemon -g

As you can see, you install the package as normal and only add the -g option. Since the package is locally installed on your computer, it won’t show up in the package.json file.

Dev dependencies

NPM packages can be installed as dev dependencies. These are packages that are not used in the application, but rather tools which helps in development.

These are installed by using the --save-dev option

Here is an example:

npm install karma --save-dev

Karma is a package used for testing your code. In the package.json file, you can see that this package got its own json-property:

"devDependencies": {
  "karma": "^4.4.1"

So to sum it up:

The difference between dev dependencies and regular dependencies is that dev dependencies are not necessary for the application to run

For instance, you can’t run an express server application without express, but you can run the application without a code testing package like Karma.

You can also use this shorthand command to add dev dependencies:

npm install karma -D

How to install a specific version of an NPM Package

For instance, as I write this, the latest version of lodash is 4.17.15.

You might want to install an older version if the latest version is not compatible with other dependencies in your project.

Let’s say we want to install version 4.17.5. You do that by adding @{version number} after the package name like this:

npm install lodash@4.17.5

You can view the version history from the NPM website.

How to Find Outdated NPM Packages

To see which dependencies in your project are outdated you can use this command:

npm outdated

This will give you an overview of your outdated packages like this:

To see which NPM packages are outdated globally you can use this command:

npm outdated -g

How to Update NPM Packages

To update the lodash package in your project do this:

npm update lodash

The command above also works for dev dependencies.

To update globally do this:

npm update nodemon -g

How to Uninstall NPM Packages

To uninstall packages you simply use the uninstall command. Let’s uninstall lodash from our project

npm uninstall lodash

The dependency will be removed from the package.json file.

To uninstall global packages you do this:

npm uninstall nodemon -g

Similar Posts