Skip to content
ClapelloNotes by Barath
ReactCh 02 · 2/6
Core4 min readUpdated

React / 05

Versions, the lockfile and node_modules

TL;DR

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.

BumpExampleMeaning
Patch1.4.2 → 1.4.3Bug fixes only
Minor1.4.2 → 1.5.0New features, nothing breaks
Major1.4.2 → 2.0.0Breaking 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 writeAllowsIn plain words
"^1.4.2">=1.4.2 <2.0.0Minors and patches, never the next major
"~1.4.2">=1.4.2 <1.5.0Patches only
"1.4.2"exactly 1.4.2Nothing 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.

Pick a range from package.json. Lit versions are allowed; npm installs the newest lit one, unless a lockfile already pins a version.

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.

package-lock.json (a small part)
"node_modules/react": {
  "version": "19.3.1",
  "resolved": "https://registry.npmjs.org/react/-/react-19.3.1.tgz",
  "integrity": "sha512-…"
}
package.jsonpackage-lock.json
HoldsRanges you accept (^19.3.1)Exact versions (19.3.1)
CoversOnly packages you asked forEvery package in the tree
Written byYou (and npm install <pkg>)npm, automatically
Commit it?YesYes

npm install vs npm ci

CommandUse itWhat it does
npm installWhile developingInstalls from package.json, follows the lockfile when it can, updates the lockfile if needed
npm ciCI servers, deploysDeletes 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

You list two packages. They bring their own dependencies (transitive dependencies), which bring theirs. npm installs the whole tree.

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:

.gitignore
node_modules

Gotchas

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.