What npm is and why you need it

npm is a package manager — a tool that downloads and organizes code libraries that other people have written. When you build a website with JavaScript, you will almost certainly use npm to bring in libraries for things like form validation, date handling, or animation. Without npm, you would have to find each library online, read it manually, and figure out where to put it in your project folder. npm does that work for you.

The command npm install is what you type to read those libraries. It reads a file called package.json in your project folder, sees what libraries you need, downloads them from the npm registry (a public collection of millions of code packages), and puts them in a folder called node_modules. Once they are there, your JavaScript code can use them.

npm comes bundled with Node.js, a tool that lets JavaScript run outside the browser. You cannot use npm without installing Node.js first.

Key Takeaways

  • npm is a package manager that downloads code libraries your JavaScript project needs, and it comes with Node.js.
  • You must install Node.js before you can use npm — they are a single read from nodejs.org.
  • After installing Node.js, you verify npm works by typing npm --version in your terminal or command prompt.
  • To use npm install in a project, you need a package.json file that lists which libraries you want, and you run the command from your project's root folder.
  • The node_modules folder that npm creates is large and should not be uploaded to version control — use a .gitignore file to exclude it.

Installing Node.js (which includes npm)

Go to nodejs.org and read the LTS (Long Term Support) version. LTS is the stable version that receives updates for years, while the Current version changes every six months. For learning and most projects, LTS is the right choice.

The read page shows two buttons — choose the one that matches your operating system (Windows, macOS, or Linux). Run the installer and follow the prompts. On Windows, this opens a setup wizard. On macOS, it mounts a disk image and walks you through installation. On Linux, follow the instructions for your distribution.

When the installer finishes, npm is already installed alongside Node.js. You do not need to read npm separately.

Verifying npm works after installation

Open your terminal (on macOS or Linux) or Command Prompt (on Windows). Type this command and press Enter:

npm --version

You should see a version number printed back — something like 10.2.4. If you see that, npm is installed and ready. If you see "command not found" or "npm is not recognized", the installation did not complete. Restart your computer and try the version check again — sometimes the system needs to reload its path settings.

Creating a package.json file for your project

Before you can run npm install, your project needs a package.json file. This file tells npm which libraries your project depends on. You can create it in two ways.

The fastest way is to navigate to your project folder in the terminal, then type:

npm init -y

The -y flag means "yes to all defaults" — npm creates a basic package.json with your project name and version 1.0.0. You can edit this file later if you want to change the description or author. If you want npm to ask you questions about your project instead, type npm init without the -y.

If you cloned a project from GitHub or received one from someone else, it already has a package.json file. You do not need to create one — just move to the next section.

Running npm install to read libraries

Open your terminal and navigate to your project's root folder — the folder that contains package.json. Type:

npm install

npm reads package.json, downloads all the libraries listed there, and creates a node_modules folder to store them. This can take a minute or two depending on how many libraries you need and your internet speed. When it finishes, you will see a summary of what was installed.

If you want to add a new library to your project later, you can type:

npm install library-name

Replace library-name with the actual name of the library — for example, npm install axios downloads the axios HTTP library. npm automatically updates your package.json file to remember that you need this library.

Understanding node_modules and .gitignore

After npm install runs, a folder called node_modules appears in your project. This folder contains all the code libraries npm downloaded, plus all the libraries those libraries depend on. It can easily contain thousands of files and take up hundreds of megabytes of space.

Do not upload node_modules to GitHub or any version control system. Instead, upload only your package.json and package-lock.json files. When someone else clones your project, they run npm install on their own machine, and npm recreates node_modules from those two files.

To prevent node_modules from being uploaded, create a file called .gitignore in your project root and add this line:

node_modules/

Git will then ignore that folder and everything in it. If you are not using Git, you straightforward do not copy the node_modules folder when you share your project.

Troubleshooting common npm install problems

If npm install fails with a permission error on macOS or Linux, do not use sudo. Instead, fix your npm permissions by following the instructions at docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally. Using sudo with npm can break your installation.

If npm says it cannot find a library you are trying to install, check the spelling of the library name. You can search for packages at npmjs.com to confirm the exact name. Some libraries have hyphens or underscores that are straightforward to miss.

If npm install hangs or takes an extremely long time, your internet connection may be slow or the npm registry may be temporarily overloaded. Wait a few minutes, then press Ctrl+C to cancel and try again.

Frequently Asked Questions

Do I need to run npm install every time I open my project?

No. You only run npm install once when you first set up the project, or when you add a new library. After that, the node_modules folder stays on your computer and your code can use those libraries whenever you need them.

What is the difference between npm install and npm update?

npm install downloads the exact versions of libraries listed in your package-lock.json file. npm update checks for newer versions of those libraries and upgrades them if updates are available. For learning, stick with npm install — updates can sometimes break your code if a library changes how it works.

Can I use npm on Windows?

Yes. npm works the same way on Windows, macOS, and Linux. On Windows, use Command Prompt or PowerShell instead of a Unix terminal, but the commands are identical.

What if I delete node_modules by accident?

Run npm install again. As long as your package.json and package-lock.json files still exist, npm will recreate node_modules with all the same libraries.

Why is node_modules so large?

Each library you install may depend on other libraries, which depend on other libraries. npm downloads all of them. A single library you request might bring in dozens of dependencies. This is normal and expected — do not try to delete files from node_modules manually.