Working with npm Packages
Explore how to install npm packages locally and globally, manage dependencies through the package.json file, and use the axios library to simplify HTTP requests. Understand the distinction between dependencies and devDependencies, and apply practical skills to integrate external libraries into your Node.js projects.
In the previous lesson, we explored npm and the package.json file, which helps organize a project’s metadata and scripts. However, the true strength of npm lies in its ability to manage dependencies—external libraries that enhance our projects by providing ready-made solutions for common tasks, saving both time and effort.
For example:
chalkfor styling terminal text with colors.axiosfor simplifying HTTP requests.jsonwebtokenfor secure, token-based authentication.
Installing packages
Before using an external library in our project, we must install it. npm makes this process seamless, whether installing locally for a specific project or globally for system-wide tools.
Installing a package locally
Local installations are tied to a specific project and are the most common way to use npm packages. They allow us to include libraries specific to our project's needs without affecting other projects. For example, we can install the axios library to simplify making HTTP requests in our application.
npm install axios
After running this command, the following happens:
-
npm adds the
axioslibrary to anode_modulesdirectory in our project folder. This folder contains all locally installed packages and their dependencies. -
npm updates the
package.jsonfile, ...