Staying safe: lockfiles, pinned versions, and what a supply-chain attack is
See why a range in package.json can bring a different version tomorrow, read the lockfile’s version, address and fingerprint, install exactly what it names with npm ci, pin a version when you need to, and follow the real attack on chalk of September 2025 to see how a supply-chain attack travels and what a lock does and does not stop.
Fundamentals
TypeScript
Staying safe: lockfiles, pinned versions, and what a supply-chain attack is. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
On 8 September 2025, at 13:13 UTC (Coordinated Universal Time, the
world's reference clock), a new version of chalk appeared on npm:
5.6.1. chalk is the package that colours text in the terminal, the one
boxen brought into TaskKit. It passed every question of the last
lesson: hundreds of millions of downloads a week, looked after, types
built in. And this version had been published by an attacker, from the
npm account of one of its maintainers, the people who look after
chalk and publish it. That day a fake email from "npm" had tricked
one of them into handing the account over. For about an hour and a
half, every fresh install of a project that used boxen 8.0.1, the
newest boxen that day, got the attacker's chalk. TaskKit was not one
of them: its boxen 9 came out a year later and asks for chalk 6.
A shopping list that says "bread" gets you whatever bread the shop has
today. A receipt names the loaf you bought, down to its barcode, so you
can buy the same one again. npm keeps both kinds of list for TaskKit.
By the end of this lesson TaskKit installs exactly the versions you
checked, on any computer, and you can say how an attack like this one
travels to a program and what stops it.
^9.0.0 is a range, not a version
TaskKit's package.json says "boxen": "^9.0.0". The caret, as
lesson 9.2 showed, means "this version or any newer one with the same
first number". That is a range: every version that fits the rule,
and it grows each time boxen's authors publish a newer 9.x version.
Packages write ranges too. boxen 9.0.0 asks for chalk ^6.0.0, a
range that boxen's authors wrote, not you. So when npm installs
TaskKit with only package.json to read, it takes the newest version
that fits each range, at that moment. Install today and next month,
and you may get two different chalks from the same package.json.
Two files answer two different questions: package.json, and
package-lock.json, TaskKit's lockfile, the lock for short:
package.json
Does: Says which versions of each package TaskKit accepts
Never: It never says which version you actually got
package-lock.json
Does:
Records the exact version of every package npm installed, and where it
came from
Never: It never picks a newer version by itself
package.json says which versions TaskKit accepts; the lock
says which one it got
The lockfile: a receipt for every package
The lock is the record of exactly what npm installed. npm writes it on
every npm install, and lesson 1.4 told you never to edit it by hand.
Open it in VS Code (Visual Studio Code, your editor) now. It is long,
over 600 lines, because it has one entry for every package npm knows
TaskKit needs, chalk included, though TaskKit's package.json never
names it. To jump to boxen's entry, press these keys and type
"node_modules/boxen":
⌘ Cmd+F
Ctrl+F
Ctrl+F
VS Code's Find box. Press Escape to close it.
The entry's first three lines are the ones that matter, the last one
broken here to fit:
"version" is the exact version npm installed. "resolved" is the
address of the file it downloaded: a whole package packed into one
file, which is what .tgz means (bundled with a tool called tar, and
shrunk with gzip). "integrity" is the package's fingerprint: a
long code that npm works out from every byte of that file (sha512
names the method, SHA for Secure Hash Algorithm). Change one character
in the file and the code comes out different.
When npm installs from the lock, it downloads the file named in
"resolved", works out its fingerprint again, and compares the two. A
file that does not match is refused with npm error code EINTEGRITY,
and nothing is installed. So the lock does not only remember a version
number: it remembers what that version's file looked like.
That makes package-lock.json part of TaskKit, as much as index.ts
is. It goes wherever the project goes: when you copy TaskKit to
another computer, or put it online in Module 15, the lock goes too.
npm ci: exactly what the lock says
The two files are in sync when every package package.json asks
for has an entry in the lock, and every version in the lock still fits
its range. Plain npm install then keeps the lock's versions. But its
job is to make node_modules match package.json, and when the two
files disagree, it changes the lock to match. For the times you want
the receipt and nothing else, npm has a second command, npm ci,
short for clean install. In the taskkit folder, run:
Every package was installed already, and npm still says it added 20.
npm ci first empties node_modules, then installs exactly what the
lock names, each file checked against its fingerprint. It never changes
package.json or the lock. The lock has more entries than npm added:
it also lists TypeScript built for other systems, and npm installs only
the one for yours. The time, in milliseconds (ms), will differ, and so
may the counts if your versions are newer.
As in lesson 14.1, the last line is npm looking up every version it
installed in a list of known flaws that an attacker could use. It found
none.
Which command does what depends on the state the lock is in. When
npm ci stops, it is refusing to guess:
npm installnpm ciIn sync
Thelock'sversions
Installs what the lock names, nothing newer
Exactlythelock
Starts from empty, then installs what the lock names
Not in sync
Rewritesthelock
Picks versions that fit, and writes them in
So npm install is for changing what TaskKit has: adding a package,
taking one out. npm ci is for getting back what TaskKit already has:
on a new computer, after copying the project, or any time
node_modules looks wrong.
That last case is where people reach for the wrong fix: they delete
package-lock.json and node_modules in VS Code, then install again.
Bad — with the lock gone, every
package jumps to the newest version that fits today.
Good — the lock stays, and you get
back exactly the versions it names.
In a project whose package.json says "chalk": "^5.6.0" and whose
lock named 5.6.0, the Bad steps left chalk 5.6.2. Both fill an empty
node_modules; only the second fills it with the versions you checked.
Pinned versions
Lessons 1.4 and 9.2 said the lock pins versions. A line of
package.json can be pinned too, and that matters if the lock is ever
lost: then the ranges decide again. A pinned version is one version
with no caret in front. --save-exact asks npm to write it that way:
npm installs nothing new, since 9.0.0 is already there; it only
rewrites the line. package.json now says "boxen": "9.0.0". That
line never moves by itself, lock or no lock. It does not pin what boxen
brings: boxen's own chalk ^6.0.0 is still a range, and only the lock
holds the chalk you got. And it costs you fixes: a new boxen 9 that
repairs a bug never arrives until you change the line yourself.
TaskKit keeps npm's caret: the lock and npm ci already hold every
package, the ones you never name included. If you tried the command
above in TaskKit, put the caret back with npm install boxen, which
prints the same lines and writes "boxen": "^9.0.0" again. Pinning
helps when the lock cannot be trusted to stay, or for a package whose
every update you want to choose by hand.
What a supply-chain attack is
Bread reaches the shop along a chain: a farm, a mill, a bakery, a
truck. Spoil one link, and every loaf after it is spoiled, though the
shop did nothing wrong. Code reaches TaskKit the same way, and a
supply-chain attack goes after one of those links instead of your
program: a package, the account of the person who publishes it, or the
registry. Nobody broke into any of the projects in this lesson's
opening. The attacker took one login with phishing: an email made
to look like npm's, asking the maintainer to sign in on a copy of npm's
website, which kept the password and the one-time code the login
asked for.
The figure follows that day down the chain: one lane for the attacker,
then one for each link it passed, the registry, npm, and a project that
trusts boxen, which trusts chalk. A fresh install, as in the story
above, is one made with no lock, straight from the ranges. Switch
between it and a project that installed from its lock, and press
Next step to walk through each, or let it play:
The same login published bad versions of other packages that day,
among them debug, a package that prints messages about what a
program is doing. People noticed within hours: chalk's other
maintainer published 5.6.2, the clean code again, at 14:47, and npm
removed 5.6.1. Removing it from npm did not take it out of anyone's
node_modules, though. A project that got 5.6.1 kept it, its lock
naming 5.6.1, until it installed again and wrote a good version into
the lock. The hidden
part went after cryptocurrency payments made in web browsers, so a
terminal program like TaskKit would have carried it without harm. The
next attack does not have to be that narrow.
The lock protected every project that already had a good chalk in it.
It does not protect the moment you install or update: whatever npm
installs then, the lock writes down faithfully, the attacker's version
included. So one habit is worth keeping. Before you install or update
a package, look at Last publish on its npmjs.com page (lesson
14.1). If its newest version is a day old, wait a few days: most bad
versions are found and removed in that time, by the people who
installed them first.
Predict before you run
A project's package.json and its lock are in sync, with these two
lines. Today the newest chalk 5 on npm is 5.6.2.
Predict
You run plain npm install. Which version of chalk do you have?
The common mistake: a dependency typed by hand
package.json is a file you can type in, so sometimes a dependency
goes in by hand rather than through npm install.
Common mistake
Adding a package to package.json by hand
A guide shows the line for date-fns, so you paste it into
"dependencies" yourself:
Then you install the way this lesson taught:
npm stops and installs nothing. Its first lines, broken here to fit
(after them it prints a list of npm ci's options, which you can
ignore):
EUSAGE is npm's code for "the command cannot be used like this", and
npm-shrinkwrap.json is another name a lock can have; TaskKit's is
package-lock.json. package.json asks for a package the lock has
never seen, and npm ci never changes the lock.
`npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
The fix
Let npm install do the work it is for: it installs date-fns and
writes it into the lock. After that, the two files are in sync again,
and npm ci works.
not in sync means package.json changed and the lock did not. The
Missing: line names the package. If you did not mean to add it, take
the line back out of package.json instead; if you did, npm install
it, ideally by name, npm install date-fns, so you never type the line
yourself.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
These run in your browser, so they ask about npm and its lock rather
than run it.
Recap
Recap · 3 questions
Check yourself
What does package-lock.json record?
Check yourself
What is a supply-chain attack?
Check yourself
At 13:30 UTC on 8 September 2025 you ran npm install chalk.
Did your lock protect you?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Staying safe: lockfiles, pinned versions, and supply-chain
attacks
What things are
Word
What it means
Range, ^9.0.0
Every version that fits; without a lock, the newest
Lockfile
package-lock.json: what npm installed, exactly
Fingerprint
"integrity": a code worked out from the file
In sync
package.json and the lock agree
Pinned version
"9.0.0" with no caret: one version only
Maintainer
The person who looks after a package and publishes it
Supply-chain attack
An attack on something your program trusts
Phishing
A fake message that asks for your password
Commands and messages
Write or see
What it means
npm ci
Empty node_modules, install exactly the lock
npm install
Add a package, and update the lock
--save-exact
Pin it: npm install --save-exact boxen
EINTEGRITY
A file does not match its fingerprint: refused
Not in sync
package.json changed: run npm install
Rules
Rule
What it means
Keep the lock
It goes wherever the project goes
Never edit it
npm writes it; you add packages with
Where to go next
You can now tell a range from a version, read the lock's version,
address and fingerprint for any package, set up TaskKit anywhere with
npm ci, pin a version when you need to, and say how a supply-chain
attack travels and where the lock stops it.
Next is chalk: colors in the terminal, where TaskKit installs
chalk, the package from this lesson's story, by name for the first
time, and its output gets colours.
Before you move on, open package-lock.json, find boxen's entry and,
under its "dependencies", the range it asks for chalk. Which version
of chalk does the lock name for it? Then run npm run check and
npm start in taskkit, to see that npm ci changed nothing in how
TaskKit works.
Stops
Changes nothing: the two files are not in sync
No lock
Thenewestthatfit
Takes the newest version of every range, writes a new lock
Stops
Changes nothing: it needs a lock to install from
npm install keeps the lock up to date; npm ci installs it,
or stops
An email that looks like npm's asks a chalk maintainer to log in on
a lookalike site.
The maintainer types their password and one-time code there, and
the attacker has their npm account.
13:13 UTC: a new chalk is published from that account. It is the
real code, plus a hidden part.
The project uses boxen 8, which asks for chalk ^5.3.0. The
newest version that fits is now 5.6.1.
npm downloads it and puts it in node_modules, with no warning.
Every run loads the hidden part too, until the project installs
a good version again.
One stolen login reaches every fresh install; a lock keeps
the version you checked
All 6 steps shown.Step 1 of 6: Attacker: fake emailStep 2 of 6: Attacker: login stolenStep 3 of 6: Registry: chalk 5.6.1Step 4 of 6: npm: 5.6.1 fits ^5.3.0Step 5 of 6: npm: gets 5.6.1Step 6 of 6: A project: runs 5.6.1
All 6 steps shown.
Attacker
Registry
npm
Aproject
Attacker: fake emailfakeemailfakeemail
Attacker: login stolenloginstolenloginstolen
Registry: chalk 5.6.1chalk5.6.1chalk5.6.1
npm: reads the lockreadsthelockreadsthelock
npm: chalk 5.6.0 ✓chalk5.6.0✓chalk5.6.0✓
A project: runs 5.6.0runs5.6.0runs5.6.0
Time
An email that looks like npm's asks a chalk maintainer to log in on
a lookalike site.
The maintainer types their password and one-time code there, and
the attacker has their npm account.
13:13 UTC: a new chalk is published from that account. It is the
real code, plus a hidden part.
package-lock.json names chalk 5.6.0, where to download it, and
its fingerprint.
npm downloads 5.6.0 and its fingerprint matches. It never asks
for the newest version.
The project runs the version you checked, as it did yesterday.
One stolen login reaches every fresh install; a lock keeps
the version you checked
All 6 steps shown.Step 1 of 6: Attacker: fake emailStep 2 of 6: Attacker: login stolenStep 3 of 6: Registry: chalk 5.6.1Step 4 of 6: npm: reads the lockStep 5 of 6: npm: chalk 5.6.0 ✓Step 6 of 6: A project: runs 5.6.0
All 6 steps shown.
npm install
Never delete it
Deleting it lets every range jump to its newest
Your packages' packages
Only the lock holds their versions
Let it wait
A day-old version can wait a few days
Exercise · Read the code
Pinned, and still changed
A project pinned boxen in its package.json, "boxen": "8.0.1", but
had no lock. Someone installed it at 14:00 UTC on 8 September 2025.
boxen 8.0.1 asks for chalk ^5.3.0. Which chalk did they get?
Exercise · Read the code
What the fingerprint catches
The lock names chalk 6.0.1 with its "integrity". Someone replaces the
file at the "resolved" address with a changed one, still called
6.0.1. You run npm ci. What happens?
added 20 packages, and audited 21 packages in 656ms14 packages are looking for funding run `npm fund` for detailsfound 0 vulnerabilities
Outputtext
up to date, audited 21 packages in 915ms14 packages are looking for funding run `npm fund` for detailsfound 0 vulnerabilities
Outputtext
npm error code EUSAGEnpm errornpm error `npm ci` can only install packages when your package.json andpackage-lock.json or npm-shrinkwrap.json are in sync. Please updateyour lock file with `npm install` before continuing.npm errornpm error Missing: date-fns@4.4.0 from lock file
You copied the taskkit folder, lock included, to a new laptop. This
program prints your plan for getting it ready, but its first two steps
throw the lock away. Fix it, so it prints npm ci, npm run check
and npm test, in that order.
Loading the editor…
Exercise · Write the code
Which versions you get
Write the body of whatYouGet. It takes the command and whether the
project has a lock that is in sync, and gives back one of three
answers: the lock's versions, the newest versions or stops. The
program prints the answer for every pair; the checks compare each one
with what npm really does.