What you'll learn
Quick Answer
npm (Node Package Manager) is the default tool for installing and managing reusable code packages in JavaScript projects that run on Node.js. It reads a file called package.json to know which packages your project needs, downloads them into a node_modules folder, and lets you run handy command shortcuts called scripts. npm ships with Node.js, so once you install Node, you already have npm.
What Is npm?
If you are learning JavaScript and keep seeing commands like npm install in tutorials, this section is for you. So, what is npm? npm stands for Node Package Manager. It is the tool most developers use to download and manage packages — small pieces of reusable code that other people have written and shared, so you do not have to build everything yourself.
Think of npm as an app store for code. Need to format dates, make a web server, validate a form, or draw a chart? Instead of writing that logic from scratch, you run one command and npm fetches a ready-made, tested package for you. There are more than a million packages available, covering almost anything you can imagine.
npm does three main jobs:
- Installs packages your project depends on.
- Tracks exactly which packages and versions you are using, in a file called
package.json. - Runs scripts — short command shortcuts you define, like starting your app or running tests.
The word "npm" refers to both the command-line tool on your computer and the online registry (at npmjs.com) where all those packages live.
npm vs Node.js: What's the Difference?
Beginners often mix these two up, so let us clear it up early. They are related but not the same thing.
- Node.js is the runtime — the program that actually runs JavaScript code outside of a web browser (for example, on a server or your laptop's terminal).
- npm is the package manager — the tool that installs and organises the extra code your Node.js project needs.
Here is the key point that saves confusion: npm comes bundled with Node.js. When you install Node.js from nodejs.org (choose the LTS version — it is the stable one), you automatically get npm too. You do not install them separately.
You can check both are installed by opening your terminal and running:
node -v
npm -vIf each command prints a version number, you are ready to go. If you want to understand the runtime itself in depth, our free Node.js course walks you through it step by step with hands-on lessons.
The package.json File
Every npm project has a file called package.json at its root. This is the ID card of your project. It records the project's name, version, and — most importantly — the list of packages it depends on. When someone else (or a server) gets your project, this one file tells npm exactly what to download.
You create it with a single command:
npm init -yThe -y flag says "yes to all the default answers" so you do not have to fill in a form. A basic package.json looks like this:
{
"name": "my-first-app",
"version": "1.0.0",
"description": "My first npm project",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {},
"devDependencies": {}
}The two most useful parts for beginners are scripts (your command shortcuts) and dependencies (the packages you have installed). We will fill both of these in shortly.
npm install: Local vs Global
The command you will type most often is npm install (you can shorten it to npm i). To add a package to your project, you run:
npm install <package-name>For example, npm install dayjs. npm downloads the package into a folder named node_modules and adds a line to package.json so the choice is recorded.
There are two places a package can be installed:
- Local (the default) — the package lives inside this project's
node_modulesfolder and is saved in itspackage.json. Use this for anything your code willrequireorimport. - Global — using the
-gflag, the package is installed once for your whole computer, not tied to any project:npm install -g typescript. Use this only for command-line tools you want to run anywhere.
| Question | Local (default) | Global (-g) |
|---|---|---|
| Saved in your package.json? | Yes | No |
| Shared across all projects? | No | Yes |
| Right for project libraries? | Yes | No |
| Right for CLI tools? | Partial | Yes |
Recommendation: install locally by default. Reach for global installs rarely. For a tool you only need once in a while, prefer npx (explained later) so you avoid cluttering your system.
Dependencies vs devDependencies
Look again at package.json and you will see two lists: dependencies and devDependencies. The difference is simple once you know it.
- dependencies — packages your app needs to run. If your app talks to a database or formats dates while it is live, those go here. This is the default when you run
npm install express. - devDependencies — packages you only need while developing: testing tools, code formatters, auto-reloaders. Your finished, running app does not need them. Add these with the
--save-dev(or-D) flag:
npm install --save-dev nodemonWhy bother separating them? On a production server you can run npm install --production to skip the dev tools, which keeps the install smaller and faster. For a package like nodemon (a tool that restarts your app when you save a file) this makes perfect sense — it helps you build, but the live app does not need it.
A quick rule of thumb: if the running program uses it, it is a dependency; if only you use it while coding, it is a devDependency.
Running npm Scripts
Typing long commands again and again gets tiring. npm scripts let you save a command under a short name inside the scripts block of package.json:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "echo \"No tests yet\""
}Now you can run these shortcuts. There is one small gotcha worth remembering:
startandtestare special — you can run them directly:npm startandnpm test.- Every other script name needs the word
run:npm run dev.
So npm start works, but for your dev script you must type npm run dev — a very common beginner mistake. Scripts are powerful because they hide the messy details. A teammate who clones your project does not need to know the exact command; they just run npm start and it works the same way on every machine.
Walkthrough: Your First Package
Let us put it all together with a tiny project that prints today's date in a friendly format. Every command here actually works — try it. First, create a folder and set up the project:
mkdir my-first-app
cd my-first-app
npm init -yNow install a real, popular package called dayjs, a small library for working with dates:
npm install dayjsNotice what changed: a node_modules folder appeared, a package-lock.json file was created, and dayjs is now listed under dependencies in your package.json. Next, create a file named index.js with this content:
const dayjs = require('dayjs');
console.log('Today is ' + dayjs().format('DD MMMM YYYY'));Run it with Node:
node index.jsYou should see something like Today is 22 July 2026. That is it — you just used a package that someone else wrote, installed through npm, in about a minute. Notice you never opened the package's source code. You trusted npm to fetch it, and you called it with require. That is the everyday rhythm of working with npm.
Common Gotchas and a Clear Recommendation
A few things trip up almost every beginner. Keep these in mind:
- Never commit node_modules to Git. It can hold thousands of files and can be rebuilt any time with
npm install. Addnode_modulesto a.gitignorefile instead. - Do commit package-lock.json. This file locks the exact versions you installed, so your teammates and your server get identical packages. It prevents "works on my machine" bugs.
- Understand the ^ symbol. A version like
"dayjs": "^1.11.0"means "1.11.0 or any newer 1.x version". This is why the lock file matters — it pins the real version. - npm vs npx.
npminstalls packages;npxruns a command-line package once without installing it globally. For example,npx create-react-app my-appruns the tool and cleans up after itself.
Our recommendation for beginners: install the Node.js LTS version, keep installs local by default, always commit package-lock.json, and use npx for one-off tools. Master these basics and you will handle almost any real-world JavaScript project with confidence. When you are ready to build servers and APIs with these skills, continue with our free Node.js course.
Frequently Asked Questions
Is npm free to use?
Yes. npm and the public package registry are completely free. You can install and publish public packages without paying anything. npm does offer paid plans for private packages used by teams and companies, but you never need those to learn or to build normal projects.
Do I need to install npm separately from Node.js?
No. npm comes bundled with Node.js. When you download and install Node.js from nodejs.org, you automatically get the npm command too. You can confirm both are ready by running node -v and npm -v in your terminal.
What is the difference between npm and npx?
npm installs and manages packages in your project. npx runs a package's command a single time without installing it permanently. For example, npx create-react-app my-app runs the setup tool once, which is perfect for tools you only need occasionally.
Should I commit the node_modules folder to Git?
No. The node_modules folder is large and can always be recreated by running npm install, since your package.json and package-lock.json record everything needed. Add node_modules to your .gitignore file so it stays out of version control.
Are npm, yarn, and pnpm the same thing?
They are alternatives that do the same core job: managing JavaScript packages. npm is the default that ships with Node.js, so it is the best place to start. yarn and pnpm are popular alternatives that some teams prefer for speed or disk usage, but the concepts you learn with npm carry over directly.
