Tutorial

Adding and Removing Packages Using npm or Yarn

Published on September 22, 2019
Default avatar

By joshtronic

Adding and Removing Packages Using npm or Yarn

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking 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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
joshtronic

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel