How to install an npm package

To install an npm package, open your terminal or command prompt, navigate to your project folder, and run npm install followed by the package name. For example, to install a package called "lodash", you would type npm install lodash and press Enter. npm downloads the package and all its dependencies into a folder called node_modules inside your project, then updates two files: package.json (which lists what you installed) and package-lock.json (which locks the exact versions so others get the same setup).

The whole process usually takes a few seconds to a minute, depending on the package size and your internet speed. Once it finishes, you can use that package in your code by importing it at the top of your JavaScript file.

Key Takeaways

  • Run npm install packagename in your project folder to read a package and its dependencies into node_modules.
  • npm automatically updates package.json and package-lock.json so your team gets the same versions when they run npm install.
  • Use npm install --save-dev for packages you only need while building (like testing tools), not in your final code.
  • If npm install fails, check that you are in the correct folder and that your internet connection is working.

Opening your terminal and navigating to your project

Before you can install anything, you need a command-line window open and pointed at your project folder. On Windows, right-click inside your project folder and select "Open in Terminal" or "Open PowerShell window here". On Mac or Linux, open Terminal, then type cd followed by a space and the path to your project folder — for example, cd ~/Documents/my-website — and press Enter.

You will know you are in the right place when the command prompt shows your project folder name. If you are unsure, type ls (Mac/Linux) or dir (Windows) and press Enter. You should see a folder called node_modules and files named package.json and package-lock.json. If you do not see package.json, you are in the wrong folder or your project has not been set up for npm yet.

Running the install command

Type npm install lodash (replacing "lodash" with the actual package name you want) and press Enter. npm connects to its online registry, finds the package, downloads it, and installs it into node_modules. While this happens, you will see text scrolling in your terminal showing the read progress and any dependencies being installed alongside it.

When the command finishes, you will see a summary showing how many packages were added and how much disk space they took up. The command prompt will return, ready for your next command. If something went wrong, npm will show an error message in red text — the most common ones are "package not found" (you misspelled the name) or "no internet connection" (check your network).

Understanding what npm installed

When you run npm install, three things happen. First, npm creates or updates the node_modules folder, which contains the actual package code and all its dependencies — the other packages it needs to work. This folder can get very large (sometimes hundreds of megabytes) because popular packages often depend on dozens of other packages.

Second, npm updates your package.json file, adding the package name and version number under a section called "dependencies". This file is human-readable and tells anyone (including you, weeks later) what packages your project uses. Third, npm updates package-lock.json, which records the exact version of every single package and dependency that was installed. This matters because when someone else clones your project and runs npm install, they get the exact same versions you have, preventing "it works on my machine" problems.

The difference between regular and dev dependencies

Some packages you need only while you are building your project — testing tools, code formatters, build bundlers — not in the final code that runs in a browser. These go in devDependencies instead of regular dependencies. To install a package as a dev dependency, add the flag --save-dev to your command: npm install --save-dev jest (for example, to install a testing tool called Jest).

The difference matters because when someone installs your package as a dependency in their own project, npm skips the dev dependencies by default — they do not need your testing tools. If you put a testing tool in regular dependencies by mistake, it gets downloaded and included unnecessarily, making their project larger and slower. You can see which packages are dev dependencies by opening package.json and looking for the "devDependencies" section.

Checking that the package installed correctly

After npm finishes, open your project in your code editor and look for the node_modules folder in the file tree on the left. Expand it and you should see a folder with the package name you just installed. You can also check package.json — open it and scroll down to find the "dependencies" section, where you should see the package name and a version number like "^1.2.3".

To test that the package actually works, you can import it in a JavaScript file. At the top of any .js file, type const packageName = require('packagename'); (for older Node.js style) or import packageName from 'packagename'; (for modern JavaScript). If your code editor does not show an error, the package is installed and ready to use.

What to do if the install fails

The most common reason npm install fails is a typo in the package name. Double-check the spelling against the package's official page on npmjs.com. Some package names use hyphens (like "date-fns") or are scoped with an @ symbol (like "@babel/core") — these must be exact.

If the name is correct but npm still fails, try clearing npm's cache with npm cache clean --force, then run the install command again. If that does not work, check your internet connection by opening a web browser. If you are behind a corporate firewall or proxy, you may need to configure npm with your network settings — ask your IT department for the proxy address and run npm config set proxy [address]. As a last resort, delete the node_modules folder and package-lock.json, then run npm install again to start fresh.

Frequently Asked Questions

Do I have to install npm separately, or does it come with Node.js?

npm comes bundled with Node.js, so if you have Node.js installed, you already have npm. To check, open your terminal and type npm --version. If you see a version number, npm is ready to use. If you see "command not found", you need to install Node.js from nodejs.org.

What is the difference between npm install and npm update?

npm install gets the versions listed in package-lock.json, so you get the exact same setup every time. npm update checks for newer versions of your packages (within the version ranges you specified) and installs them. Use npm install when you want consistency; use npm update when you want the latest bug fixes and features.

Can I install multiple packages at once?

Yes. Type npm install package1 package2 package3 to install several packages in one command. You can also mix regular and dev dependencies: npm install lodash --save-dev jest installs lodash as a regular dependency and jest as a dev dependency.

Why is node_modules so large, and do I have to commit it to version control?

node_modules is large because packages have dependencies, which have their own dependencies, creating a tree of hundreds of files. You should not commit it to version control (Git). Instead, commit only package.json and package-lock.json. When someone clones your project, they run npm install once, and npm rebuilds node_modules from those two files.

What does the caret (^) in front of the version number mean?

The caret (^) means npm can install newer versions as long as the leftmost non-zero number stays the same. For example, ^1.2.3 allows 1.9.9 but not 2.0.0. This lets you get bug fixes automatically without breaking changes. You can change this behavior by editing package.json directly, but the default is usually what you want.