React / 05
Versions, the lockfile and node_modules
What ^ and ~ mean, why package-lock.json pins exact versions for everyone, and why node_modules never goes into git.
Version numbers mean something
Packages use semantic versioning (semver): three numbers, MAJOR.MINOR.PATCH.
| Bump | Example | Meaning |
|---|---|---|
| Patch | 1.4.2 → 1.4.3 | Bug fixes only |
| Minor | 1.4.2 → 1.5.0 | New features, nothing breaks |
| Major | 1.4.2 → 2.0.0 | Breaking changes: your code may need updating |
^ and ~ are ranges
In package.json, the symbol in front of a version says which updates you accept. npm install writes ^ by default.
| You write | Allows | In plain words |
|---|---|---|
"^1.4.2" | >=1.4.2 <2.0.0 | Minors and patches, never the next major |
"~1.4.2" | >=1.4.2 <1.5.0 | Patches only |
"1.4.2" | exactly 1.4.2 | Nothing else |
What does this range allow?
Try it>= 1.4.2 and < 2.0.0
Caret: new features and fixes, but not the next major version.
published on npm
- 1.4.1 (not allowed)
- 1.4.2 (allowed)
- 1.4.9 (allowed)
- 1.5.0 (allowed)
- 1.9.3 (installed)
- 2.0.0 (not allowed)
A fresh npm install picks 1.9.3.
package-lock.json pins exact versions
Ranges are flexible on purpose. The lockfile removes that flexibility for installs: it records the exact version, download URL and a checksum (integrity) of every package that was installed.
"node_modules/react": {
"version": "19.3.1",
"resolved": "https://registry.npmjs.org/react/-/react-19.3.1.tgz",
"integrity": "sha512-…"
}package.json | package-lock.json | |
|---|---|---|
| Holds | Ranges you accept (^19.3.1) | Exact versions (19.3.1) |
| Covers | Only packages you asked for | Every package in the tree |
| Written by | You (and npm install <pkg>) | npm, automatically |
| Commit it? | Yes | Yes |
npm install vs npm ci
| Command | Use it | What it does |
|---|---|---|
npm install | While developing | Installs from package.json, follows the lockfile when it can, updates the lockfile if needed |
npm ci | CI servers, deploys | Deletes node_modules, installs exactly the lockfile, fails if it disagrees with package.json. Never changes the lockfile |
To update on purpose: npm outdated shows what's newer, npm update updates within your ranges, and npm install react@latest jumps to the newest version, even a new major.
node_modules and transitive dependencies
node_modules is where the actual code lands. It's big because packages depend on other packages. Those are transitive dependencies: you never asked for them, but you need them.
you asked for
react-dom
^19.3.1
scheduler
^0.26.0
react-dom needs it, so you get it too
you asked for
vite
^8.3.6
rolldown
~1.2.18
postcss
^8.5.32
lightningcss
^1.33.0
…and more
each with its own dependencies
Every folder inside node_modules is a package with its own package.json. npm flattens the tree where it can, so shared packages are installed once. Because node_modules can be rebuilt any time from package.json + the lockfile, it never goes into git:
node_modulesGotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
