Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Tools
Back to What Is
What is NPM and How Does it Work?

What is NPM and How Does it Work?

What IsBy Muhammad Fareed
npm

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

  1. You run npm install express.
  2. npm looks up the package in the registry and picks a version that matches the range you asked for.
  3. It also resolves every package that package depends on.
  4. It downloads everything into the node_modules folder.
  5. It records the dependency in package.json and the exact versions in package-lock.json.

The files npm manages

File or folderPurposeCommit to Git?
package.jsonProject metadata, scripts and dependency rangesYes
package-lock.jsonExact resolved versions for repeatable installsYes
node_modules/The downloaded packagesNo

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

TypeNeeded whenExamples
dependenciesThe app runs in productionexpress, react
devDependenciesOnly while developing or buildingjest, 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:

RangeAllows
^4.18.2Any 4.x.x version at or above 4.18.2
~4.18.2Only 4.18.x patches
4.18.2That 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 audit regularly 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 ci in 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.