Skip to the content

Lesson 98 of 296

npm and package.json

What you will learn

  • What npm and packages are
  • package.json, field by field
  • dependencies vs devDependencies
  • Version numbers and ^ vs ~
  • package-lock.json and npm ci
  • Scripts
  • npx
  • Security and maintenance
  • pnpm and yarn
On this page

What is npm?

A package is reusable code published for others to use, such as React, Express or date-fns. npm (Node Package Manager) installs packages and comes with Node.js. The npm registry is the public online store of packages.

Packages your project uses are called dependencies. They are installed into a node_modules folder.

Starting a project

mkdir my-app && cd my-app
npm init -y

This creates package.json.

package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "node --watch src/server.js",
    "start": "node src/server.js",
    "test": "vitest",
    "lint": "eslint ."
  },
  "dependencies": {
    "express": "^5.1.0",
    "zod": "^3.25.0"
  },
  "devDependencies": {
    "eslint": "^9.0.0",
    "vitest": "^3.0.0"
  },
  "engines": {
    "node": ">=20"
  }
}

(Version numbers here are examples. Always install the current versions.)

FieldMeaning
name, versionThe project's name and version
privatetrue stops it being published to npm by accident
type"module" makes .js files ES modules (Lesson 73)
scriptsNamed commands you run with npm run
dependenciesPackages needed to run the app
devDependenciesPackages needed only to develop and build: tests, linters, build tools, TypeScript
enginesWhich Node.js versions the project supports

Installing

CommandMeaning
npm install (or npm i)Install everything listed in package.json
npm install expressAdd a dependency
npm install -D vitestAdd a devDependency
npm uninstall expressRemove a package
npm install -g some-cliInstall a command-line tool globally (use sparingly)

Version numbers: semantic versioning

Most packages use semantic versioning (SemVer): MAJOR.MINOR.PATCH, for example 5.1.3.

PartIncreases whenExample
MAJORBreaking changes: your code may need updating4.x.x to 5.0.0
MINORNew features, still compatible5.1.0 to 5.2.0
PATCHBug fixes, still compatible5.1.3 to 5.1.4

^ and ~ in package.json

WrittenAllows updates toExample: allows
^5.1.3Same major5.1.4, 5.9.0, but not 6.0.0
~5.1.3Same minor5.1.4, 5.1.9, but not 5.2.0
5.1.3Exactly this versionOnly 5.1.3

npm adds ^ by default. (For versions starting with 0, such as ^0.4.2, ^ only allows patch updates, because 0.x versions may break things at any minor change.)

package-lock.json

package.json says "any compatible version". package-lock.json records the exact version of every package installed, including the packages your packages depend on.

  • Commit package-lock.json. It makes every developer and every server install exactly the same versions.
  • Do not commit node_modules. It is recreated from the lock file.

npm install vs npm ci

npm installnpm ci
UsesYour daily workCI servers and production builds
Lock fileMay update itInstalls exactly what it says; fails if it does not match package.json
node_modulesUpdates itDeletes it and installs fresh

Use npm ci in CI pipelines and Dockerfiles for repeatable builds (Part 19).

Scripts

npm run dev
npm run lint
npm test          "test" and "start" do not need "run"
npm start

Scripts can use tools installed in node_modules without a global install. Scripts named pre<name> and post<name> run automatically before and after <name>.

npx

npx runs a package's command without installing it globally:

npx create-vite@latest my-app
npx prettier --write .

It uses the local version if the project has one, or downloads it temporarily.

Security and maintenance

CommandMeaning
npm outdatedShow packages with newer versions
npm updateUpdate within the ranges in package.json
npm auditReport known security vulnerabilities
npm audit fixApply compatible security fixes

Good habits:

  • Every dependency is code you trust and must maintain. Before adding one, check that it is actively maintained, widely used, and that you really need it.
  • Watch for typosquatting: fake packages with names very close to popular ones.
  • Some packages run install scripts when installed. Malicious packages use this to attack computers. Install only packages you trust.
  • Use tools such as Dependabot or Renovate to keep dependencies updated through PRs.

pnpm and yarn

Alternative package managers that do the same job with different trade-offs:

  • pnpm: very fast, saves a lot of disk space by sharing packages between projects, and is strict about undeclared dependencies. Popular in monorepos.
  • yarn: an older alternative to npm with its own lock file.

Use one package manager per project. Do not mix lock files (package-lock.json, pnpm-lock.yaml, yarn.lock).

Common mistakes

  • Committing node_modules, or not committing the lock file.
  • Putting build and test tools in dependencies instead of devDependencies.
  • Using npm install in CI instead of npm ci.
  • Running sudo npm install for project packages.
  • Adding a large library for something a few lines of code could do.

How to say it in an interview

"npm installs packages listed in package.json into node_modules. dependencies are needed at runtime and devDependencies only for development and builds. Versions follow semantic versioning; a caret allows minor and patch updates and a tilde only patches. package-lock.json pins the exact tree, so I always commit it, never node_modules, and use npm ci in CI for reproducible installs. I run tasks through npm scripts, use npx for one-off tools, and keep dependencies few, audited and updated, being aware of supply-chain risks."

Key words

WordMeaning
PackageReusable code published to a registry
npmNode.js's package manager
DependencyA package your project uses
devDependencyA package needed only for development
Semantic versioningMAJOR.MINOR.PATCH version numbers
Lock fileRecords the exact versions installed
npm ciA clean, exact install from the lock file
npxRuns a package's command without a global install
Supply-chain attackMalicious code delivered through a dependency

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.