Adding and Removing Packages Using npm or Yarn


While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the "report an issue" button at the bottom of the tutorial.

One of the beautiful things about both Open Source and the ecosystems of modern programming languages is that there’s a good chance the code you’re about to write has already been written.

There are a plethora of packages out there for Node.js and between you and me, they are usually written by folks smarter than myself that have thought through of a bunch of stuff I wouldn’t have even dreamed of. Standing on the shoulders of giants, as they say.

Getting started

For this article, I’m going to discuss using both npm and yarn. If you’re an avid reader of our reptilian friendly site, you have probably seen both commands mentioned in our other Node.js articles.

For those new to the party, npm and yarn are package managers for Node.js. They both leverage the package.json file for your projects and function quite similarly.

If you already have Node.js installed locally, you probably have npm installed. If you’d prefer to follow along using yarn, you can check out their installation instructions here.

Depending on your system, you could also consult with your friendly neighborhood package manager and install things that way.

Also, we’re going to be installing things globally as well as to a project as a dependency. You could very well use an existing project of yours, or you could create a dummy project out in your /tmp directory as such:

$ mkdir /tmp/gator-project
$ cd /tmp/gator-project
$ npm init -y

This creates a package.json file that we will be adding and removing packages from.

Adding a Development Dependency to a Project

Not all dependencies are created equal, as some are only required while doing development. These dependencies, while important, can slow down production deployments since they take time to install and the code will never be touched.

Examples of development dependencies would be testing utilities like mocha or jest. For those types of dependencies, we can install them as such, and have them added to the devDependencies section of our package.json:

# With NPM
$ npm install --save-dev mocha
# Shorthand version
$ npm i -D mocha

# With Yarn
$ yarn add --dev mocha
# Shorthand version
$ yarn add -D mocha

Adding a Production Dependency to a Project

Other dependencies are mission critical to the application and should always be installed regardless if it’s a development environment or not. We call these production dependencies and tend to includes packages like express or react.

Adding a production dependency to a project is just as easy as adding a development one, but it will be added to the dependencies section of our package.json instead:

# With NPM
$ npm install --save express
# Shorthand version
$ npm i -P express

# With Yarn
$ yarn add express

Installing a Package Globally

Sometimes you want to install a package outside of your current project, so it’s available to all of the projects on your system. These are installed globally and are great for packages that also include command-line utilities that you want to run alongside your other command-line utilities:

# With NPM
$ npm install --global json
# Shorthand version
$ npm i -g json

# With Yarn
$ yarn global add json

Removing a Dependency From a Project

In every project’s life, there comes a time when a dependency that once seemed like a good idea, no longer serves any purpose. Don’t be too sad, deleting code is always a good thing (assuming you have proper test cover to ensure you didn’t break anything).

To remove either a development or production dependency from a project, we simply uninstall or remove it:

# With NPM
$ npm uninstall jest
# Shorthand version
$ npm r jest

# With Yarn
$ yarn remove jest

This will remove things from node_modules as well as drop the dependency from our package.json. Depending on your version of either command, you may also see updates to your lock file.

Uninstalling a Package Globally

Removing a globally installed package is the same as removing one from a project, but we need to pass in the global argument as we did when installing it:

# With NPM
$ npm uninstall --global json
# Shorthand version
$ npm r -g json

# With Yarn
$ yarn global remove json
Creative Commons License