Table of Contents
In Node projects, installing dependencies is a fundamental part of the development process. In this post, we'll learn how to install npm packages.
package.json
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:
BASHnpm 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:
BASHnpm install <package>
You can also do this using yarn:
BASHyarn 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:
BASHnpm install <package> --save-dev
This is how you do this in yarn:
BASHyarn 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:
BASHnpm install <package> -g
This is how you do this in yarn:
BASHyarn global add <package>
Conclusion
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!
Resources
- Getting Started with TypeScript
- How to Install Node on Windows, macOS and Linux
- Managing PHP Dependencies with Composer
- Create an RSS Reader in Node
- Best Visual Studio Code Extensions for 2022
- How to deploy a MySQL Server using Docker
- Using Puppeteer and Jest for End-to-End Testing
- Getting Started with Handlebars.js
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Using Push.js to Display Web Browser Notifications
- Setting Up a Local Web Server using Node.js
- Using Axios to Pull Data from a REST API