What npm Does and Why You Need It

npm is a tool that downloads and installs code packages — pre-written chunks of JavaScript that other developers have shared — so you don't have to write everything from scratch. When you're building a website with HTML, CSS, and JavaScript, you'll often need a package to handle something like date formatting, image optimization, or form validation. npm fetches that package from the internet and puts it in a folder on your computer where your project can use it.

npm stands for Node Package Manager. It comes bundled with Node.js, which is a way to run JavaScript outside the browser — on your computer or a server. If you want to use npm, you first install Node.js, and npm comes along automatically.

The alternative is writing every function yourself or copying code from websites into your project files. npm saves you that work and keeps your project organized because all your packages live in one place and npm tracks which versions you're using.

Key Takeaways

  • npm is a package manager that downloads and installs JavaScript code libraries so you can use them in your project without writing them yourself.
  • You install npm by downloading and installing Node.js from nodejs.org, which includes npm automatically.
  • Every project gets its own folder with a package.json file that lists which packages the project needs and what versions.
  • The command npm install reads your package.json and downloads all the packages your project depends on into a node_modules folder.
  • You add a new package to your project with npm install package-name, which updates package.json and downloads the code.

Installing Node.js and npm on Your Computer

Go to nodejs.org and read the version labeled LTS (Long Term Support). LTS versions are stable and receive updates for longer, which matters if you're learning. Click the read button, run the installer, and follow the prompts — accept the default settings unless you have a reason to change them.

Once the installer finishes, open your terminal or command prompt. On Windows, search for "Command Prompt" or "PowerShell". On Mac, search for "Terminal". On Linux, open your terminal process. Type this command and press Enter:

npm --version

If you see a version number like "10.2.4", npm is installed and working. If you see "command not found" or an error, restart your computer and try again — sometimes the system needs to reload after installing Node.js.

Creating a Project Folder and Initializing npm

Create a new folder on your computer where your project will live. Open your terminal, navigate to that folder, and run this command:

npm init -y

The -y flag tells npm to use default settings and skip questions. This command creates a file called package.json in your folder. Open it in a text editor and you'll see something like this:

{ "name": "my-project", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }

This file is npm's record of your project. Every time you install a package, npm updates this file to remember what you installed and which version. You don't edit this file by hand — npm does it for you when you install packages.

Installing Your First Package

Let's say you want to use a package called lodash, which provides useful functions for working with JavaScript data. In your terminal, from inside your project folder, type:

npm install lodash

npm connects to the internet, downloads lodash and everything it depends on, and puts all the code into a folder called node_modules inside your project. This takes a few seconds. When it finishes, look at your project folder — you'll see a new node_modules folder and your package.json file has been updated with a new section called "dependencies" that lists lodash and its version number.

Now you can use lodash in your JavaScript files. At the top of a JavaScript file, write:

const _ = require('lodash');

This line tells JavaScript to load lodash and give it the nickname _. After that, you can use any function lodash provides in your code.

Understanding node_modules and .gitignore

The node_modules folder contains thousands of files — sometimes hundreds of megabytes. You should never commit this folder to version control (like Git) or upload it to the internet. Instead, you commit only your package.json file. When someone else clones your project, they run npm install and npm reads package.json, downloads all the packages you listed, and rebuilds node_modules on their computer.

To prevent accidentally uploading node_modules, create a file called .gitignore in your project folder (the dot at the start is important). Inside it, write a single line:

node_modules

If you're using Git, this tells it to ignore the node_modules folder. If you're not using version control yet, this file doesn't hurt — it just sits there ready for when you do.

Installing Multiple Packages and Checking What You Have

You can install several packages in one command by listing them with spaces:

npm install lodash moment axios

This installs three packages at once. npm updates package.json to list all three under dependencies.

To see what packages your project currently has, type:

npm list

This shows a tree of all your packages and their versions. If you only want to see the top-level packages you installed (not the packages they depend on), type:

npm list --depth=0

You can also open package.json in a text editor and look at the "dependencies" section — it's a simpler view of the same information.

Removing a Package You No Longer Need

If you install a package and later decide you don't need it, remove it with:

npm uninstall package-name

Replace package-name with the actual name, like npm uninstall lodash. npm deletes the package from node_modules and removes it from package.json. Your project when ready stops having access to that package.

If you're working on a project someone else started and you want to install all the packages listed in their package.json, just run:

npm install

With no package name after it, npm reads package.json and downloads everything listed there. This is how you set up a project after cloning it from GitHub or receiving it from a teammate.

Frequently Asked Questions

What's the difference between npm install and npm install package-name?

npm install with no package name reads your package.json file and downloads all the packages already listed there. npm install package-name downloads a specific new package, adds it to package.json, and installs it. Use the first when setting up a project for the first time; use the second when adding a new package to an existing project.

Why is node_modules so large?

When you install a package, npm also installs every package that package depends on, and every package those depend on, and so on. A single package you install might pull in hundreds of other packages. This creates a large folder, but it's normal and expected. That's why you don't upload it — package.json is much smaller and contains all the information npm needs to rebuild it.

Can I use npm packages in a website I'm building for the browser?

Yes, but you need a bundler like Webpack or Parcel to combine your npm packages into a single JavaScript file the browser can load. For learning, you can start with straightforward packages that work in Node.js. Once you're comfortable with npm, you can learn bundlers to use packages in browser-based websites.

What if npm install fails or gets stuck?

First, check that you have an internet connection. If the connection is fine, try clearing npm's cache with npm cache clean --force, then run npm install again. If it still fails, delete your node_modules folder and package-lock.json file, then run npm install once more. This forces npm to start fresh.

Do I need to install npm for every project?

No. You install Node.js and npm once on your computer. After that, every project folder gets its own package.json and node_modules folder. npm itself stays installed and works for all your projects.