Intro to Node Installing Node Packages

Learning objective: By the end of this lesson, students will learn the basics of installing and using an npm package from the npm registry.

Working with packages in a project

Now that your project has a package.json file, you can add packages from the npm registry!

Installing a package

We’ll use a popular and practical package called validator for our example. Let’s follow that link and check out some of the documentation for the package. It offers a variety of string validation and sanitization methods, such as:

These kinds of methods might be helpful to us if we were collecting information from a user and needed to verify that it matches a specific format. While we could write this code ourselves, using a package someone else has already written saves us time!

To add a package from npm to your project, use the command:

npm i <package-name>

Replacing <package-name> (including the < and >) with the name of the package. As an example, to install the validator package we’ve been discussing, run the following command now:

npm i validator

🧠 Note: The i in npm i is a shorthand for install.

The installation process

Exploring package.json post-installation

After installing a package, the dependencies object in package.json is updated to reflect this change:

{
  "name": "intro-to-node",
  "version": "1.0.0",
  ...
  "dependencies": {
    "validator": "^13.11.0"
  }
}

Reinstalling node modules

There might be situations where you need to delete and reinstall all your project’s dependencies. npm makes it easy by keeping an inventory of your packages in the package.json file.

Reinstalling is as easy as running npm install (or npm i for short).

By running npm i, all dependencies listed in package.json are automatically downloaded.

Let’s try it!

🎓 You Do

  1. First, delete the node_modules directory. Don’t worry. You can safely delete this entire directory without losing your project’s dependency information.

  2. Reinstall it by running the command:

    npm i
    

    Observe how npm automatically recreates the node_modules directory with all the necessary packages based on the package.json.

Keeping node_modules out of Git repositories

As you install more dependencies, the node_modules directory can become large. Keeping it out of Git repositories saves space and makes cloning the repository faster.

🧠 To prevent Git from tracking your node_modules, add it to a .gitignore file. This file tells Git which directories and files to ignore.

Uninstalling packages

npm makes it easy to manage your packages and delete or reinstall when necessary. To remove a package from your project, use:

npm uninstall <package-name>

Replacing <package-name> (including the < and >) with the name of the package. As an example, to uninstall the validator package, run this command now:

npm uninstall validator

This command deletes the package from your node_modules directory. It also removes validator from the package.json file, ensuring that your project and its dependency list remain synchronized.