What is NPM and How Does it Work?
Short answer: npm (Node Package Manager) is the default tool for installing and managing JavaScript packages. It is three things in one: a public registry of packages, a command-line tool, and a company that runs both. It comes bundled with Node.js.
What npm does for you
- Downloads libraries your project depends on.
- Records exactly which versions you use, so teammates get the same result.
- Runs project tasks such as build and test through scripts.
- Lets you publish your own code for others to install.
How installing works
- You run
npm install express. - npm looks up the package in the registry and picks a version that matches the range you asked for.
- It also resolves every package that package depends on.
- It downloads everything into the
node_modulesfolder. - It records the dependency in
package.jsonand the exact versions inpackage-lock.json.
The files npm manages
| File or folder | Purpose | Commit to Git? |
|---|---|---|
package.json | Project metadata, scripts and dependency ranges | Yes |
package-lock.json | Exact resolved versions for repeatable installs | Yes |
node_modules/ | The downloaded packages | No |
See every field in understanding package.json.
Essential commands
npm init -y # create package.json
npm install express # add a dependency
npm install -D jest # add a development dependency
npm install # install everything listed in package.json
npm uninstall express # remove a package
npm update # update within allowed ranges
npm outdated # show packages with newer versions
npm audit # check for known vulnerabilities
npm run build # run a script from package.json
Many have short aliases; see shorthands for npm commands.
Dependencies vs devDependencies
| Type | Needed when | Examples |
|---|---|---|
dependencies | The app runs in production | express, react |
devDependencies | Only while developing or building | jest, eslint, typescript |
Semantic versioning
Package versions look like MAJOR.MINOR.PATCH, for example 4.18.2. A major bump signals breaking changes, a minor adds features, and a patch fixes bugs. Ranges control which updates are allowed:
| Range | Allows |
|---|---|
^4.18.2 | Any 4.x.x version at or above 4.18.2 |
~4.18.2 | Only 4.18.x patches |
4.18.2 | That exact version |
Local vs global installs
Most packages belong in the project (local). Install globally only for command-line tools you use everywhere:
npm install -g typescript
For one-off tools, npx runs a package without installing it permanently, for example npx create-next-app.
Security basics
- Run
npm auditregularly and update vulnerable packages. - Prefer well-maintained packages with many users.
- Be careful with typos in package names; fake look-alike packages exist.
- Use
npm ciin CI so installs match the lock file exactly.
Alternatives
Yarn and pnpm use the same registry and offer different trade-offs. Compare them in npm vs Yarn vs pnpm.
Frequently asked questions
Is it safe to install any package from npm?
Not automatically. Anyone can publish packages, so check download counts, maintenance activity, the repository and the license, and run npm audit regularly to find known vulnerabilities.
What is the package-lock.json file for?
It records the exact versions of every installed package and where they came from, so each developer and server installs identical dependencies. Commit it to version control.
What is the difference between dependencies and devDependencies?
dependencies are needed to run the app in production, while devDependencies are only for development tasks such as testing, linting and building. Installing with --omit=dev skips the latter.
Next steps
Automate tasks with npm scripts, then learn how to publish an npm package. New to Node itself? Read what Node.js is.
