Packages, Versions and Lockfiles
Read a package.json, explain what a caret range may install, and say why the committed lockfile and a dependency audit keep a project reproducible and safe to upgrade.
Contentsand lesson path
On this page
- Why it matters
- package.json: the manifest
- Versions and ranges
- The lockfile
- Dependency audits
- Check your understanding
- Apply it
- Review prompt
Lessons of Frontend Tooling & Builds
- Packages, Versions and Lockfiles Concept · 20 min
- Inside a Vite Project: Dev Server, Production Build and Composed Apps Worked example · 30 min
- Linting, Formatting and a Test Runner as Guardrails Worked example · 20 min
- Lab: From Empty Folder to a Linted, Tested, Built Project Lab · 30 min
- Pairing with an AI Coding Assistant: Context, Review and Verification Worked example · 25 min
- Check: Read a package.json, a Build Report and an AI-Generated Diff Knowledge check · 8 questions
What you will learn
- Manage dependencies with npm or pnpm: package.json scripts, semantic-version ranges, committed lockfiles, dev vs runtime dependencies, and vulnerability audits.
01. Why it matters
A frontend project is more than the files you write. The tools that build, check and test it — Vite, ESLint, Prettier, Vitest — are packages: code other people published on the npm registry, each with its own version history. npm is the package manager that installs them, and it runs on Node.js, which is why Node.js and a package manager sit underneath almost every frontend toolchain (this skill was tested with Node 22.23; Node 26 is the current Active LTS). pnpm is a popular alternative with the same core ideas and its own lock file, pnpm-lock.yaml; everything in this lesson applies to it too.
The list you built in the DOM and events skill worked as loose files in a folder. To turn it into a project that other people — and other machines — can build and test, you need three things: a manifest saying what the project is and what it depends on, version ranges saying how its dependencies may change, and a lockfile saying exactly which versions you last tested with. This lesson takes each in turn.
02. package.json: the manifest
Every npm project starts with a package.json. It records the project name, how its JavaScript is loaded ("type": "module" tells Node.js and Node-based tools to treat the project's .js files as ES modules, allowing import/export in tool configs; the browser decides module loading from <script type="module">), the commands you run, and two dependency lists. The scripts section gives each command a short name: npm run dev runs what "dev" names, and a few names such as test work without run as well. Because the commands live in the manifest instead of in a wiki page, every tool — a teammate, a CI job, an AI assistant — can find and run them the same way.
The two dependency lists answer "when is this code needed?" dependencies are needed when your app runs for users: code that is shipped to the browser, or executed by a server. devDependencies are needed while developing: compilers, bundlers, linters, formatters and test runners. Vite, ESLint, Prettier and Vitest are all dev dependencies — the finished bundle does not contain them, but without them you cannot produce the bundle. This is the manifest from a small list project we use throughout the skill:
{
"name": "shopping-list",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"lint": "eslint .",
"format": "prettier --write .",
"format:check": "prettier --check .",
"test": "vitest run"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"eslint": "^10.15.0",
"globals": "^17.14.0",
"prettier": "^3.10.1",
"vite": "^8.4.0",
"vitest": "^5.1.2"
}
}The carets in the version numbers are semantic versioning ranges, and they are the next piece of the picture.
03. Versions and ranges
Every package version has three parts: MAJOR.MINOR.PATCH, such as 5.0.5. A major bump signals breaking changes, a minor bump adds features without breaking, and a patch fixes bugs. Ranges let your manifest say "keep me current within limits" instead of pinning one number:
^5.0.5(caret) allows anything from 5.0.5 up to but not including 6.0.0: minor and patch updates.~5.0.5(tilde) allows only patch updates, up to but not including 5.1.0.5.0.5with no prefix allows exactly that version and nothing else.
You can ask the registry which versions satisfy a range. On 26 November 2026, nine releases satisfied ^5.0.0; the list grows each time a new 5.x version is published:
$ npm view 'vitest@^5.0.0' version
[email protected] '5.0.0'
[email protected] '5.0.1'
[email protected] '5.0.2'
[email protected] '5.0.3'
[email protected] '5.0.4'
[email protected] '5.0.5'
[email protected] '5.1.0'
[email protected] '5.1.1'
[email protected] '5.1.2'When you install, npm resolves the range to the highest version it currently allows and records that choice. The range in the manifest says what is compatible; it does not say what you actually ran your tests against. That gap is what the lockfile closes. npm outdated compares the installed version, the newest version your ranges allow and the newest published version, so you can see what an update would change before you take it.
04. The lockfile
package-lock.json is npm's record of one resolved install. For every package in the tree — direct dependencies and the transitive ones they pull in — it stores the exact version, the URL it came from and an integrity hash of the downloaded archive. Where the manifest specifies a range, the lockfile records the exact pinned resolution (here shown after npm audit fix resolved it):
"node_modules/lodash": {
"version": "4.18.1",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz",
"integrity": "sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj…",
"dev": true,That entry is why two people with the same manifest can still get different code: ranges resolve again at install time, so a transitive package that published a new release yesterday can slip into one machine and not another. Two commands use this file differently. npm install installs from the lockfile when it is in step with the manifest, and re-resolves when you add or change dependencies. npm ci installs exactly the lockfile — nothing else, and it refuses to run if the manifest and lockfile disagree. On a clean checkout it is the command CI uses:
$ rm -rf node_modules && npm ci
added 117 packages, and audited 118 packages in 2mBecause the lockfile makes the environment reproducible, it belongs in version control next to the code — see commit hygiene. node_modules/, by contrast, is disposable: npm ci can rebuild it from the lockfile at any time, it is enormous, and platforms differ in small ways, so it goes in .gitignore. The integrity hash also gives you a small supply-chain defence: if the registry copy of a package ever changed under the same version, the hash would no longer match and the install would fail.
05. Dependency audits
Your dependencies are code you run, and published code occasionally ships a security flaw. Security researchers publish advisories — descriptions of a flaw and the versions affected — and npm compares its advisory database against the exact tree in your lockfile with npm audit. To see a real report, a demo project (pkg-demo) deliberately installed an old copy of lodash with npm install --save-dev [email protected]; older lodash releases have known flaws. Advisories are added over time, so a run on another day can list more:
$ npm audit
# npm audit report
lodash <=4.17.23
Severity: high
Command Injection in lodash - https://github.com/advisories/GHSA-35jh-r3h4-6jhm
Prototype Pollution in lodash - https://github.com/advisories/GHSA-p6mc-m468-83gw
...
1 high severity vulnerability
To address all issues, run:
npm audit fix
$ echo $?
1Two details matter. First, npm audit exits with a non-zero code when it finds a vulnerability it has not fixed, so a CI step that runs it fails the build and the failure is visible — the same mechanism that makes lint and test failures block a merge. Second, the severity describes the package, not your exposure: a flaw in a function you never call is less urgent than the same flaw on your hottest path. npm audit fix upgrades within your ranges, which is why it moved lodash from 4.17.15 to a patched 4.x release here; it only touches other versions with --force, and a forced major upgrade is a breaking change that deserves the same testing as any upgrade:
$ npm audit fix
found 0 vulnerabilities
$ npm ls lodash
[email protected]
└── [email protected]06. Check your understanding
07. Apply it
Work on the shopping-list practice for this skill, starting from the list you built in the DOM skill:
- Run
npm init -yin the project folder and set"type": "module". Read the generated manifest and delete anything that does not describe your project. - Install Vite, ESLint, Prettier and Vitest with
--save-dev, then showdevDependenciesand confirm every tool is in the development list. - For each tool, run
npm view <name>@'<your range>' versionand read the list of versions your range allows. Compare it with the version in your lockfile. - Run
npm audit. If it is clean, read the output anyway and note what a clean report looks like; commit the lockfile and addnode_modules/to.gitignore.
08. Review prompt
Without looking back: what do the caret, tilde and bare version ranges each allow? What does npm ci do differently from npm install, and what does it refuse to do? Why is Vite a devDependency while a data-fetching library would not be? And what two things does the lockfile record that the manifest does not?
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.