Working with npm Packages
Learn how to install and use external packages from the npm registry to enhance our 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:
chalk
for styling terminal text with colors.axios
for simplifying HTTP requests.jsonwebtoken
for 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
axios
library to anode_modules
directory in our project folder. This folder contains all locally installed packages and their dependencies. -
npm updates the
package.json
file, creating or modifying adependencies
section:"dependencies": { "axios": "^1.7.8" }
The dependencies section in package.json
is a critical part of npm's functionality. It ...