Managing dependencies
Overview
Very few Node projects are written from scratch. A typical API uses a web framework, a database driver, a validation library, a logger and a dozen development tools — and each of those has dependencies of its own. Managing them well keeps your app reproducible, up to date and secure.
Installing packages
npm install express # runtime dependency (short form: npm i express)
npm install -D eslint # development-only dependency
npm install pg@8 # a specific major version
npm uninstall lodash # remove one
npm install # install everything listed in package.jsonpackage.json records what you asked for:
{
"dependencies": {
"express": "^5.1.0",
"pg": "^8.16.0"
},
"devDependencies": {
"eslint": "^9.30.0"
}
}dependencies vs devDependencies
- dependencies — your code
imports them while running: Express, a database driver, a logger. - devDependencies — used only while building and testing: linters, formatters, test libraries.
The difference matters in production. npm ci --omit=dev (which you’ll use in Docker) skips devDependencies, keeping production images small and reducing the code that could contain vulnerabilities.
The dependency tree
The packages you install directly are your direct dependencies. They bring their own transitive dependencies, which npm installs into node_modules too. A small project can easily end up with hundreds of packages. You can see the tree with:
npm ls # your direct dependencies
npm ls --all # the whole tree
npm explain debug # why is this package installed?Version ranges
Packages use semantic versioning: MAJOR.MINOR.PATCH.
- PATCH (5.1.1) — bug fixes.
- MINOR (5.2.0) — new features, backwards compatible.
- MAJOR (6.0.0) — breaking changes.
The range in package.json says which versions are acceptable:
| Range | Accepts | Notes |
|---|---|---|
^5.1.0 |
5.1.0 up to (not including) 6.0.0 | npm’s default |
~5.1.0 |
5.1.0 up to 5.2.0 | patches only |
5.1.0 |
exactly 5.1.0 | fully pinned |
* or latest |
anything | avoid |
^ trusts package authors to follow semver: new features and fixes are welcome, breaking changes are not.
The lock file
package-lock.json records the exact version of every package in the tree — direct and transitive — plus a checksum of each download. With it, everyone who installs gets an identical node_modules, today or in a year.
- Commit it. It’s as important as your code.
- Don’t edit it by hand. npm updates it whenever you install, update or remove a package.
- Don’t commit
node_modules. It’s large, platform-specific and fully reproducible from the lock file.
npm install vs npm ci
npm install |
npm ci |
|
|---|---|---|
| Use it | while developing | in CI, Docker and deployments |
| Lock file | may update it | must match package.json exactly, never changes it |
node_modules |
adds to it | deletes it and starts fresh |
| Speed | — | faster for clean installs |
npm ci fails if package.json and the lock file disagree, which catches the classic “I forgot to commit the lock file” mistake before it reaches production.
Keeping up to date
npm outdated # what's behind, and by how much
npm update # update within the allowed ranges
npm install express@latest # move to a new major version deliberatelynpm outdated shows three columns: Current (installed), Wanted (newest allowed by your range) and Latest (newest published). When Latest has a new major version, read the package’s changelog or migration guide before upgrading — that’s where the breaking changes are listed.
Small, regular updates are far easier than a giant upgrade every two years. Tools such as Dependabot or Renovate open pull requests automatically when new versions come out, and your tests tell you whether they’re safe.
Security
npm audit # known vulnerabilities in your tree
npm audit fix # apply compatible fixesnpm audit checks your packages against a database of published vulnerabilities. Not every warning affects you (a problem in a dev-only tool rarely matters in production), but read them, and fix the ones in runtime dependencies promptly.
Supply-chain hygiene
Each package you install can run code on your machine and in production. Attackers know this and publish malicious packages, or take over popular ones. Protect yourself:
- Prefer fewer dependencies. Don’t install a package for something Node already does or a few lines can do.
- Check before you install: weekly downloads, recent maintenance, open issues, and who publishes it.
- Watch for typos.
expresssorreqeustmight be impostors. - Review install scripts. Packages can run scripts on install;
npm install --ignore-scriptsdisables them when you’re unsure. - Keep the lock file committed so a compromised new version can’t sneak in silently.
A good habit: before adding a dependency, ask “what does this give me that’s worth maintaining for years?” A small, focused dependency list is a sign of a healthy project.
Dependency housekeeping
In a project, install express as a dependency and eslint as a dev dependency. Then list outdated packages, check for known vulnerabilities, and do a clean install from the lock file — the way a CI server would.
# your commands herenpm install express
npm install --save-dev eslint
npm outdated
npm audit
rm -rf node_modules
npm ciSummary
dependenciesare needed at runtime;devDependenciesonly while developing.- Version ranges like
^5.1.0allow compatible updates; the lock file records the exact versions installed. - Commit
package-lock.json; usenpm ciin CI and Docker for repeatable installs. npm outdated,npm updateandnpm auditkeep dependencies current and safe.- Every dependency is code you trust — prefer fewer, well-maintained packages.