NPM Packages and Dependencies
npm is the package manager that comes with Node.js. It installs packages (reusable libraries published by others) from the npm registry, records which ones your project uses in package.json, pins their exact versions in package-lock.json, and runs your project's scripts. Almost every JavaScript project, front end or back end, uses it (or a compatible alternative such as pnpm, Yarn or Bun). Packages save enormous amounts of work, but every package is code written by someone else that runs with your app's permissions, so using them safely matters as much as using them at all.
In One Minute
npm init -ycreatespackage.json;npm install nameadds a dependency;npm install -D nameadds a development-only one.package.jsonlists dependencies with version ranges;package-lock.jsonrecords the exact versions installed. Commit both.npm ciinstalls exactly what the lock file says: use it in CI and deployments."scripts"define commands run withnpm run name(npm testandnpm startare shortcuts).- Every dependency is a security decision: check what you add, keep it updated, and run
npm audit.
Starting a Project
mkdir fee-tracker && cd fee-tracker
npm init -y
npm install date-fns
npm install --save-dev eslintnpm init -y creates a package.json with default values. npm install date-fns downloads the package into node_modules/ and records it under dependencies; --save-dev (or -D) records a tool under devDependencies. The result:
{
"name": "fee-tracker",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node src/index.js",
"test": "node --test",
"lint": "eslint src"
},
"dependencies": {
"date-fns": "^4.1.0"
},
"devDependencies": {
"eslint": "^9.39.0"
},
"engines": {
"node": ">=26"
}
}| Field | Meaning |
|---|---|
name, version | Identify the package (required to publish) |
type | "module" makes .js files ES modules |
scripts | Named commands for npm run |
dependencies | Packages the app needs to run |
devDependencies | Packages needed only to develop: test runners, linters, build tools |
engines | Which Node.js versions the project supports |
main, exports | What other code gets when it imports this package (for libraries) |
private: true | Prevents accidental publishing |
Installing and Using Packages
| Command | Does |
|---|---|
npm install | Installs everything in package.json (and updates the lock file if needed) |
npm ci | Clean install of exactly the lock file's versions; fails if they disagree |
npm install name | Adds a dependency |
npm install -D name | Adds a dev dependency |
npm install name@4.1.0 | Installs a specific version |
npm uninstall name | Removes a package |
npm update | Updates packages within their allowed ranges |
npm outdated | Lists packages with newer versions |
npm ls name | Shows where a package sits in the dependency tree |
Installed packages are imported by name; Node.js finds them in node_modules:
import { format, addDays } from "date-fns";
console.log(format(addDays(new Date(2026, 0, 5), 30), "MM/dd/yyyy")); // 02/04/2026Never edit files inside node_modules or commit that folder: it's regenerated by npm install. Add it to .gitignore.
Scripts
npm run <name> runs a script from package.json, with node_modules/.bin on the PATH, so locally installed tools work without a global install:
npm run lint # runs: eslint src
npm test # shortcut for npm run test
npm start # shortcut for npm run start
npm run build -- --watch # arguments after -- are passed to the scriptScripts named pre<name> and post<name> run before and after <name> automatically (for example pretest). Scripts are the place to document how to work on a project: a new teammate only needs npm install and npm test.
Semantic Versioning
Package versions follow semantic versioning (semver): MAJOR.MINOR.PATCH.
| Part | Increased when | Example |
|---|---|---|
| MAJOR | Breaking changes | 3.4.2 → 4.0.0 |
| MINOR | New features, backward compatible | 4.0.0 → 4.1.0 |
| PATCH | Bug fixes, backward compatible | 4.1.0 → 4.1.1 |
package.json usually stores a range, not an exact version:
| Range | Allows | For ^4.1.0 / ~4.1.0 |
|---|---|---|
^4.1.0 (caret, the default) | Minor and patch updates | 4.1.0 up to, not including, 5.0.0 |
~4.1.0 (tilde) | Patch updates only | 4.1.0 up to, not including, 4.2.0 |
4.1.0 | Exactly that version | 4.1.0 only |
>=4.1.0 | Anything newer | Any later version, including breaking ones |
Versions below 1.0.0 are treated more strictly: ^0.3.2 allows only 0.3.x, because in 0.x versions minor bumps may break things. Here is the caret rule as a small function:
function satisfiesCaret(version, range) {
const [maj, min, pat] = version.split(".").map(Number);
const [rMaj, rMin, rPat] = range.replace("^", "").split(".").map(Number);
if (maj !== rMaj) return false;
if (rMaj === 0 && min !== rMin) return false;
if (min !== rMin) return min > rMin;
return pat >= rPat;
}
for (const v of ["4.1.0", "4.9.3", "5.0.0", "4.0.9"]) console.log(v, satisfiesCaret(v, "^4.1.0"));
console.log("0.4.0", satisfiesCaret("0.4.0", "^0.3.2"));4.1.0 true
4.9.3 true
5.0.0 false
4.0.9 false
0.4.0 false(Real projects use the semver package, which handles prerelease tags and every range form.)
Lock Files
Ranges mean that two installs on different days can get different versions. package-lock.json records the exact version, download URL and integrity hash of every package in the tree, including the dependencies of your dependencies:
| Practice | Why |
|---|---|
Commit package-lock.json | Everyone, and every server, gets the same versions |
Use npm ci in CI and deployments | Installs exactly the lock file, fails if package.json and the lock disagree, never changes the lock |
| Review lock file changes in pull requests | A surprising new package or version can be an attack |
npx
npx runs a package's command without installing it permanently (or runs a locally installed one):
npx prettier --check src # uses the local prettier if installed, otherwise downloads it
npx create-vite@latest my-appSupply-Chain Security
Your app runs not only your code but every dependency, and every dependency's dependencies, often hundreds of packages. Attacks on this supply chain are real and frequent: hijacked maintainer accounts publishing malicious updates, typosquatting (reactt instead of react), and malicious install scripts that run during npm install.
| Defense | How |
|---|---|
| Add dependencies deliberately | Check downloads, maintenance, open issues, and whether you really need it; a few lines of your own code may be safer |
| Pin and lock | Commit the lock file; use npm ci |
| Audit regularly | npm audit lists known vulnerabilities; npm audit fix applies compatible fixes |
| Update thoughtfully | Use automated update tools (such as Dependabot) and review their changes |
| Limit install scripts | npm install --ignore-scripts, or allow scripts only for packages that need them |
| Verify provenance | npm audit signatures checks registry signatures and provenance attestations |
| Protect your publishing account | Two-factor authentication and trusted publishing for packages you maintain |
npm audit
npm audit --omit=dev # only what ships to production
npm audit signaturesIn Practice
The Fee Tracker's package.json for a small Node.js service: ES modules, documented scripts, a Node.js version requirement, minimal runtime dependencies, and safety settings:
{
"name": "fee-tracker-api",
"version": "1.3.0",
"private": true,
"type": "module",
"engines": { "node": ">=26" },
"scripts": {
"start": "node src/server.js",
"dev": "node --watch --env-file=.env src/server.js",
"test": "node --test",
"lint": "eslint .",
"check": "npm run lint && npm test && npm audit --omit=dev"
},
"dependencies": {},
"devDependencies": {
"eslint": "^9.39.0"
}
}This service needs no runtime dependencies at all: Node.js 26 has fetch, a test runner, --watch, .env file loading and Temporal built in. Every dependency avoided is one less thing to audit and update.
Common Mistakes
Committing node_modules. It's huge and machine-specific. Commit package.json and the lock file instead.
Not committing the lock file. Builds become unrepeatable. Commit it.
Using npm install in CI. It can change versions. Use npm ci.
Putting build tools in dependencies. They bloat production installs. Use devDependencies.
Adding packages for trivial tasks. Each one is code you must trust and maintain.
Practice
Challenge: Write parseVersion(v) that splits a semver string into numbers and isBreaking(from, to) that returns true when the major version increases (or, for 0.x, when the minor version increases).
Show solution
const parseVersion = (v) => v.split(".").map(Number);
function isBreaking(from, to) {
const [fMaj, fMin] = parseVersion(from);
const [tMaj, tMin] = parseVersion(to);
if (tMaj !== fMaj) return tMaj > fMaj;
return fMaj === 0 && tMin > fMin;
}
console.log(isBreaking("4.1.0", "4.9.0"), isBreaking("4.1.0", "5.0.0"), isBreaking("0.3.2", "0.4.0"));false true trueKey Takeaways
| Task | Command |
|---|---|
| New project | npm init -y |
| Add a dependency | npm install name |
| Add a dev tool | npm install -D name |
| Repeatable install | npm ci |
| Run a script | npm run lint |
| Check vulnerabilities | npm audit |
Was this tutorial helpful?