npm and package.json: installing packages and writing scripts
Install boxen, the first package TaskKit’s own code calls, and draw the banner with it. See why one install fills node_modules with fourteen packages, tell a dependency from a dev dependency, read the ^ in a version, and save the long check, test and run lines as scripts: npm run check, npm test and npm start.
Fundamentals
TypeScript
npm and `package.json`: installing packages and writing scripts. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Lesson 9.1 left TaskKit in four files, and you still type three
commands by hand to look after it. Before every run you check it
with tsc (short for TypeScript compiler), and npx starts it from
the project. That is
npx tsc --noEmit--types node --allowImportingTsExtensions index.ts,
sixty-seven characters, where one wrong letter means typing it
again. Then node --test, then node index.ts and the command. And
the banner at the top is still four lines you built by hand from =
signs.
Your phone solves this for long numbers: you save a number once
under a short name, and from then on you tap "Mom". A project can do
the same with its commands. By the end of this lesson TaskKit draws
its banner with a package someone else wrote. Three short commands
that npm (Node package manager) runs replace the long ones:
npm run check, npm test and npm start.
A package TaskKit runs with
Until now, every package you installed was a tool: TypeScript to
check, @types/node to describe Node to it (lessons 1.4 and 2.8).
TaskKit itself never used a line of their code. This time TaskKit
gets code it calls every time it runs: boxen, a package that
draws a frame around a piece of text.
npm downloads packages from the npm registry, a public library
of packages on the internet. Its website, npmjs.com, has a page for
each one, such as boxen's page:
what the package does, how to call it, how many people use it. In
the taskkit folder, run:
bash
npm install boxen
Outputtext
added 14 packages, and audited 19 packages in 777ms14 packages are looking for funding run `npm fund` for detailsfound 0 vulnerabilities
There is no --save-dev this time, and on purpose: you will see why
in the next section. The time, in ms (milliseconds), will differ,
and so may the counts once boxen has a newer version. "Looking for
funding" means the authors of those packages accept donations, and
npm fund lists where; you need not do anything. "Audited" means
npm compared every package in the project, your tools included, with
a public list of known security problems ("vulnerabilities"), and
found none.
You asked for one package and got fourteen. boxen does not do all
its work alone: it uses seven packages of its own, and some of
those use packages too. npm reads each package's list of what it
needs and brings it all. Press Next step to follow the install,
or let it play:
npm
registry
taskkit
1
npm to registry: boxen
2
registry to npm: boxen+alistof7
3
npm to registry: the7,andwhattheyneed
4
registry to npm: 13morepackages
5
npm to taskkit: 14intonode_modules
6
npm to taskkit: boxenintopackage.json
You ask for one package; npm brings it and everything it needs
All 6 steps shown.Step 1 of 6: npm → registry — boxenStep 2 of 6: registry → npm — boxen + a list of 7Step 3 of 6: npm → registry — the 7, and what they needStep 4 of 6: registry → npm — 13 more packagesStep 5 of 6: npm → taskkit — 14 into node_modulesStep 6 of 6: npm → taskkit — boxen into package.json
All 6 steps shown.
So node_modules is now a shelf of packages: boxen, the thirteen
it needs, and your two tools with the parts they bring. Only one
name went into package.json, though: the one you asked for.
Two lists in package.json
Open package.json in VS Code (Visual Studio Code, your editor). The
install wrote a list of its own at the bottom:
TaskKit will call boxen every time it runs, so boxen is a
dependency: a package your program needs to work, listed under
dependencies. TypeScript and @types/node help only you, while
you write and check, so they stay dev dependencies. Leave out
--save-dev for a package your code imports, and add it for a tool
you run.
The caret, ^, in front of a version reads "this one or any newer
one with the same first number". ^9.0.0 allows 9.0.1 and 9.4.0,
never 10.0.0: by an agreed rule, a package raises its first number
only when code written for the old one may stop working. npm writes a
range rather than one exact version so that a 9.0.1 that fixes a bug
can reach you, while a 10 that may break your code cannot. The exact
version npm installed is pinned in package-lock.json (lesson 1.4),
so npm install on another computer brings the same one.
A name without ./
Now use the package. Open print.ts and add this line at the very
top:
print.tsts
import boxen from "boxen";
Two things are new in that line. The text after from has no ./
in front, and that is how Node tells a package apart from a file of
yours:
"./tasks.ts" is a file in this folder, "node:test" is part of
Node itself (lesson 5.11), and a bare name such as "boxen" is a
package, which Node looks for in node_modules.
And boxen has no braces around it. A package can hand over one
main thing, called its default export, and the name in front of
from is the one you choose for it, just as
import assert from "node:assert/strict" did in lesson 5.11.
boxen's page on npmjs.com shows how to call it: the text first, then
an object of settings. It gives back the framed text as a string,
which console.log prints. Replace the whole printBanner function
in print.ts with this one, which puts the name and the version in
the frame's top edge:
print.tsts
export function printBanner(version: string): void { const title = `TaskKit ${version}`; console.log(boxen("Your tasks, in your terminal", { title: title }));}
┌ TaskKit 0.1 ───────────────┐│Your tasks, in your terminal│└────────────────────────────┘Added: MilkAdded: BreadTasks added: 2 of 5, room for 3 moreList 40% fullYour list, A to Z:#2 [todo] Bread, due: not set#1 [todo] Milk, due: not set
The check passes without a word about boxen's types. boxen brings
its own description for TypeScript; the next lesson shows what
happens with a package that does not.
Short names for long commands
Until now you let npm write package.json (lessons 1.4 and 5.11),
and its two lists of packages are still npm's job: it keeps them
matching what is in node_modules and package-lock.json. One part
npm does not fill in on its own:
scripts, a list of commands, each saved under a short name.
That part you write yourself. Add a "scripts" block under
"type", so the whole file reads:
Each script is a name in quotes, a colon, and the command in quotes.
Mind the commas: one after every line of a list except its last, and
one after the } that closes scripts. The order of the names does
not matter. package.json is JSON (JavaScript Object Notation, lesson
1.4), whose rules allow no missing comma or quote, and no comma after
a list's last line. If npm answers npm error code EJSONPARSE, one of
those is wrong, and the message names the line and the column to look
at. Save, and ask npm to run the one called check:
npm prints two lines that start with >: the script's name and the
command it is about to run. Then comes what the command itself
prints, and a clean check prints nothing, so that is all.
Look at the check line in package.json again: it says tsc, not
npx tsc. In lesson 1.4 the terminal could not find tsc on its own,
because it lives in the project's node_modules, which is not on the
PATH, the list of folders the terminal searches for programs. npm
keeps the commands its packages bring, in TaskKit only tsc, in a
folder of their own, node_modules/.bin. In a script, tsc
works. Switch between the two commands, and press Next step to
walk through each, or let it play:
You
Terminal
npm
TypeScript
You: tsc …tsc…tsc…
Terminal: find tscfindtscfindtsc
Terminal: not foundnotfoundnotfound
npm: never startsneverstartsneverstarts
Time
You type the check line without npx and press Enter.
The terminal looks for a program called tsc in its PATH.
tsc lives in taskkit/node_modules/.bin, which is not on
the PATH. The terminal gives up.
npm was never asked, so nobody puts the project's tools on the
PATH.
npm finds the line in scripts and puts the project's tools
on the PATH
All 4 steps shown.Step 1 of 4: You: tsc …Step 2 of 4: Terminal: find tscStep 3 of 4: Terminal: not foundStep 4 of 4: npm: never starts
All 4 steps shown.
You
Terminal
npm
TypeScript
You: npm run checknpmrunchecknpmruncheck
Terminal: find npmfindnpmfindnpm
npm: reads scriptsreadsscriptsreadsscripts
npm: adds .binadds.binadds.bin
TypeScript: checked ✓checked✓checked✓
Time
You type the script's name after npm run and press Enter.
npm came with Node, so the PATH has it. The terminal starts it.
npm opens package.json and finds the line under "check".
It puts node_modules/.bin, where tsc lives, first in the
PATH, and runs the line.
tsc is found without npx. It checks index.ts and the
files it imports, and prints nothing.
npm finds the line in scripts and puts the project's tools
on the PATH
All 5 steps shown.Step 1 of 5: You: npm run checkStep 2 of 5: Terminal: find npmStep 3 of 5: npm: reads scriptsStep 4 of 5: npm: adds .binStep 5 of 5: TypeScript: checked ✓
All 5 steps shown.
So a script can name any tool the project installed, with no
npx.
npm test and npm start
Two script names you will use are special: test and start are
common enough that npm runs them without the word run.
npm run test and npm run start work too.
bash
npm test
It runs node --test, after the same two > lines, and ends as
before; only duration_ms will differ:
npm start runs node index.ts, and every word you type after it
goes on the end of that command. A word that starts with -- is the
exception: npm keeps it for itself, and lesson 14.4 shows the way
past that.
bash
npm start add "pay rent" "call mom"
Outputtext
> start> node index.ts add pay rent call mom┌ TaskKit 0.1 ───────────────┐│Your tasks, in your terminal│└────────────────────────────┘Added: Pay rentAdded: Call momTasks added: 2 of 5, room for 3 moreList 40% fullYour list, A to Z:#2 [todo] Call mom, due: not set#1 [todo] Pay rent, due: not set
Look at the > line: npm shows the words without their quotes, but
TaskKit still gets two tasks, Pay rent and Call mom. A task of
two words goes in quotes, as it did after node index.ts. From now
on, these are the commands you type: npm run check, npm test,
npm start and a command.
Three of TaskKit's hints still say Try: node index.ts …: two in
command.ts and one in index.ts. They would send you back to the
long command. In command.ts, the throw under case undefined:
becomes:
command.tsts
throw new Error("Which command? Try: npm start help");
and the last throw in default becomes:
command.tsts
throw new Error(`Unknown command: ${word}. Try: npm start help`);
In index.ts, the line with Add what? becomes:
index.tsts
`Add what? Try: npm start add "buy milk"`
Then check, test, and run TaskKit with no command:
bash
npm run checknpm testnpm start
The check prints only its two > lines, and the tests end with
fail 0. TaskKit names the command you now type:
Output of npm starttext
> start> node index.ts┌ TaskKit 0.1 ───────────────┐│Your tasks, in your terminal│└────────────────────────────┘Which command? Try: npm start help
Predict the line
Here are the scripts of a different project, a small timer. Read
them, then decide what npm prints before the program itself starts.
Predict
You type npm start 25. What does npm print right after > start?
npm test and npm start work without run, so the next one
looks as if it should too.
Common mistake
Calling your own script without run
You want to check TaskKit before you run it, so you type:
bash
npm check
npm reads check as one of its own commands, such as install, and
it has none by that name. Its message goes on to point at the fix.
Unknown command: "check"
The fix
Put run between npm and any script name except test and
start:
bash
npm run check
Unknown command means the word run is missing. A different
message, Missing script: "chek", means you did write run, but
package.json has no script by that name: look for a typo in the
name you typed.
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 rather than run
it. Try any of them in TaskKit for real.
Exercise · Read the code
Which list does it go in
You are about to install two packages. dayjs will turn TaskKit's
due dates into words like "in 3 days" every time it prints the
list. prettier is a tool you run now and then to tidy the
spacing in your .ts files. Which one do you install with
--save-dev?
Exercise · Read the code
Read the caret
A friend copies TaskKit without its package-lock.json and runs
npm install. Its package.json says "boxen": "^9.0.0". Which of
these versions may npm install?
Exercise · Fix the error
Fix the import line
This program prints the line that brings boxen into print.ts, but
the line is written the way you bring in a function from a file of
your own, so TypeScript's check would refuse it. Fix it, so the
program prints the line that imports the package.
Loading the editor…
Exercise · Predict the result
Write a script
You want npm run hello to run node hello.ts. What line do you add
inside "scripts"?
Recap
Recap · 3 questions
Check yourself
npm check answers Unknown command. What is missing?
Check yourself
npm install boxen added 14 packages. Why so many?
Check yourself
Why does tsc work in a script without npx?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
npm and package.json: installing packages and writing scripts
What things are
Word
What it means
npm registry
The public library npm downloads packages from
Dependency
A package your program calls when it runs
Dev dependency
A tool you use while you write and check
^9.0.0
This version or newer, with the same first number
Script
A command saved in package.json under a name
node_modules/.bin
The commands your packages brought, tsc too
Commands
Write
To
npm install boxen
Add a dependency
--save-dev
Make it a dev dependency: npm install --save-dev prettier
npm install
Put back everything both lists name
npm run check
Run the script called check
npm test
Run the test script; no run needed
npm start add milk
Run start, with your words on the end
Rules
Rule
What it means
A bare name
from "boxen" is a package in node_modules
./
from "./tasks.ts" is a file in this folder
node:
from "node:test" is part of Node
Scripts are yours
npm keeps the lists; you write scripts
run for the rest
npm check is Unknown command
Tools by name
A script says tsc, not npx tsc
Where to go next
You can now install a package your program uses, tell a dependency
from a dev dependency, import a package by its bare name, and save
your project's long commands as scripts. TaskKit has a framed
banner and three short commands: npm run check, npm test and
npm start.
Next is Types for packages: @types and the types setting.
boxen brought its own description for TypeScript; there you install
a package that brings none, see what the check says about it, and
find the description it needs.
Before you move on, run npm start Add and npm start remove.
Say before you press Enter (Return on a Mac)
which line each one prints, then find the throw in command.ts
that wrote it.