Course contentsShow
← JavaScript
Lesson 2486 of 2,72351. Package ManagementFree lesson

Installing Packages with npm install

Using npm install to add packages to your project and how they appear in node_modules.

Installing Packages with npm install

What You'll Learn

You'll learn how to add third-party packages to your project using npm install and understand where those packages end up.

The Core Idea

Once you've initialized your project with package.json, you can pull in code that other developers have written and published to the npm registry. The command npm install (or its shorthand npm i) downloads these packages and stores them locally in your project.

Think of npm as a massive library, and npm install as checking out a book. You specify what you want, npm fetches it from the registry, and places it on your local "shelf" for your code to use.

Installing a Package

To add a package, run:

npm install lodash

This command does three things:

  1. Downloads the lodash package and all its dependencies from the npm registry
  2. Creates (or updates) a node_modules folder containing the actual code
  3. Records the dependency in your package.json under "dependencies"

The node_modules folder is where all installed packages live. It can get quite large since each package may depend on other packages, creating a nested tree of dependencies.

Common Installation Patterns

npm install express          # Add one package
npm install react react-dom  # Add multiple packages
npm install                  # Install all packages listed in package.json

The last command is crucial: when you clone a project from Git, you run npm install (without arguments) to download all the dependencies listed in the existing package.json.

What Gets Updated

After installation, your package.json gains an entry like:

"dependencies": {
  "lodash": "^4.17.21"
}

This tells other developers (and future you) what packages the project needs.

Key Takeaway: npm install <package> downloads third-party code into node_modules, records it in package.json, and makes it available for your project to import and use.