Log In

Adding and Removing Packages with npm and Yarn

Adding and Removing Packages with npm and Yarn
27.04.2024
Reading time: 4 min
Hostman Team
Technical writer

One of the main benefits of open sourced code is that most standard modules in modern programming languages have already been written, often in several different implementations. They were tested by dozens or even hundreds of programmers. The developer only needs to find and adapt the code to their own application. This significantly speeds up development, reduces the risk of errors, and allows you to release new products quickly.

The above is also true for the Node.js software platform. It is based on the V8 engine and turns JavaScript from a highly specialized tool into a general-purpose language. Various packages have been developed on it, mainly for web applications, such as chats, communication applications, and gaming programs. 

In this article, we will look at the Node.js yarn and npm utilities used for managing packages in the Node.js environment. They both access the package.json manifest file and function in a similar way. 

Prerequisites

To follow this guide, you will need:

  • A server or a computer with Node.js installed. 

On Hostman, you can deploy a cloud server with pre-installed Node.js in a few seconds. 

You don’t have to install npm and yarn separately as they will be installed on your machine with Node.js.

Creating a project

In this article, we will be installing dependencies for a particular project as well as globally. 

If you already have a project, you can use it, or you can create a new project for testing purposes, for example, in the /tmp directory:

mkdir/tmp/mynewproject
cd /tmp/mynewproject
npm init -y

The above commands will create a package.json file, from which we will add or remove packages.

Installing development dependencies

Some dependencies might be needed only at the development stage, and then they can be freely deleted. 

Typical development dependencies are testing programs like mocha or jest. You can install them with the commands:

NPM

npm install --save-dev mocha

# Or:

npm i -D mocha

Yarn

yarn add --dev mocha

# Or:

yarn add -D mocha

This will add the dependencies to the devDependencies section of the package.json file.

Installing production dependencies to a project

If a dependency is critical to the functioning of the application, it must be installed regardless of the stage, be it development or production. This category includes packages like express, react, on which the file structure, API support, user interface etc depend. 

The commands to install them are similar; however, they will be added to the dependencies section of the package.json file.

NPM

npm install --save express

# Or:

npm i -P express

Yarn

yarn add express

Installing dependencies globally

The development of similar applications makes it more convenient to install the required packages globally, so they are available for all your projects. 

This is how to install global dependencies in Node.js with npm and yarn.

NPM

npm install --global json

# Or:

npm i -g json

Yarn

yarn global add json

Removing dependencies from a project

At some point, you will need to remove a package or a few when they are no longer necessary for your project. It applies to both development and production dependencies. 

To remove dependencies from a project, use:

NPM

npm uninstall jest

# Or:

npm r jest

Yarn

yarn remove jest

The command will remove packages from node_modules and remove the dependency from package.json.

Removing dependencies globally

The same applies to global dependencies. If you completed a bunch of similar projects, there might be a few dependencies that are no longer needed. The removal procedure is similar to the previous example, but the --global option is used here, as when installing dependencies globally:

Example command:

NPM

npm uninstall --global json

# Or:

npm r -g json

Yarn

yarn global remove json

Conclusion

We described how to use npm and yarn in Node.js for managing packages, adding and removing dependencies used when developing applications on Node.js. 

The npm and yarn package managers complement the basic functionality and allow you to speed up the creation of programs on both the front-end and back-end sides.


Share