How to Add and Remove Packages using NPM and Yarn

How to Add and Remove Packages using NPM and Yarn

Learn how to add and remove packages globally and as project dependencies using npm and Yarn.

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.

I’m not here to start a flame war about which is better, but I do favor npm due to it’s ubiquity.

Fight me, loljk.

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

node-js npm

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.

Node.js Performance: Node.js vs. Io.js

You may already be aware that Raygun uses Node.JS for our API nodes that receive your precious crash reporting data (we also do node.js crash reporting if you’re interested). We’ve peaked in the past at more than 110,000 requests per second coming...

Node.js Live | Node.js Docker Tutorial | Dockerizing Node.js App|Node.js Training|Edureka

🔥 Node.js Certification Training: This Edureka video on 'Node.js Docker Tutorial' will help you in learn...