How to Install NPM Packages

Updated onbyAlan Morel
How to Install NPM Packages

In Node projects, installing dependencies is a fundamental part of the development process. In this post, we'll learn how to install npm packages.


Before we begin, you must ensure you have a package.json file in your project's root.

If you don't have one, you can create one by running the following command:

npm init -y

Once you've ran this command, you've initialized a new project and now have a package.json file.

This file is the file that contains all of the information about your project, including the dependencies you'll need to run your project.

Installing Dependencies

Once you have a package.json file, you can install dependencies by running the following command:

npm install <package>

You can also do this using yarn:

yarn add <package>

Running this command will install the specified package into your project, and add it to your package.json file. The package will be downloaded in a folder called node_modules.

If you used npm, you'll also get a package-lock.json file, which contains a list of all of the dependencies that were installed. If you used yarn, you'll get a yarn.lock file instead.

Both lock files simply keep track of the exact version of each dependency that was installed. This is so other developers can use your project and install the exact same dependencies.

Dev Dependencies

When you install a package, you have the option to install it as a dev dependency. This means that the package will be installed in your project, but it will not be needed when your project is being used in production.

This is helpful for reducing build size and increasing performance.

By default, install a project will add it to the dependencies section of your package.json file, however, you can also add it to the devDependencies section.

To add a package to the devDependencies section, you can run the following command:

npm install <package> --save-dev

This is how you do this in yarn:

yarn add <package> --dev

Install packages globally

You can also install npm packages globally. This is useful if you want to use the same package in multiple projects without having to reinstall it.

To install a package globally, you can run the following command:

npm install <package> -g

This is how you do this in yarn:

yarn global add <package>


Knowing how to install npm packages is a must for developers working in Node. We've seen all the different ways to install packages, both using npm and yarn.

Hopefully, this has helped you get a better understanding of how to install packages in Node.

Thanks for reading!


To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 All rights reserved. Made with ❤ in NY.