Working with npm Packages
Learn how to install and use external packages from the npm registry to enhance our Node.js projects.
We'll cover the following...
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, creating or modifying adependenciessection:"dependencies": { "axios": "^1.7.8" }
The dependencies section in package.json is a critical part of npm's functionality. It serves as a record of ...