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 -yThis 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.)
| Field | Meaning |
|---|---|
name, version | The project's name and version |
private | true stops it being published to npm by accident |
type | "module" makes .js files ES modules (Lesson 73) |
scripts | Named commands you run with npm run |
dependencies | Packages needed to run the app |
devDependencies | Packages needed only to develop and build: tests, linters, build tools, TypeScript |
engines | Which Node.js versions the project supports |
Installing
| Command | Meaning |
|---|---|
npm install (or npm i) | Install everything listed in package.json |
npm install express | Add a dependency |
npm install -D vitest | Add a devDependency |
npm uninstall express | Remove a package |
npm install -g some-cli | Install 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.
| Part | Increases when | Example |
|---|---|---|
| MAJOR | Breaking changes: your code may need updating | 4.x.x to 5.0.0 |
| MINOR | New features, still compatible | 5.1.0 to 5.2.0 |
| PATCH | Bug fixes, still compatible | 5.1.3 to 5.1.4 |
^ and ~ in package.json
| Written | Allows updates to | Example: allows |
|---|---|---|
^5.1.3 | Same major | 5.1.4, 5.9.0, but not 6.0.0 |
~5.1.3 | Same minor | 5.1.4, 5.1.9, but not 5.2.0 |
5.1.3 | Exactly this version | Only 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 install | npm ci | |
|---|---|---|
| Uses | Your daily work | CI servers and production builds |
| Lock file | May update it | Installs exactly what it says; fails if it does not match package.json |
| node_modules | Updates it | Deletes 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 startScripts 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
| Command | Meaning |
|---|---|
npm outdated | Show packages with newer versions |
npm update | Update within the ranges in package.json |
npm audit | Report known security vulnerabilities |
npm audit fix | Apply 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
dependenciesinstead ofdevDependencies. - Using
npm installin CI instead ofnpm ci. - Running
sudo npm installfor 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
| Word | Meaning |
|---|---|
| Package | Reusable code published to a registry |
| npm | Node.js's package manager |
| Dependency | A package your project uses |
| devDependency | A package needed only for development |
| Semantic versioning | MAJOR.MINOR.PATCH version numbers |
| Lock file | Records the exact versions installed |
| npm ci | A clean, exact install from the lock file |
| npx | Runs a package's command without a global install |
| Supply-chain attack | Malicious code delivered through a dependency |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.