See what a coloured word really is, escape codes the terminal reads as orders, install chalk by name though boxen already brought it, colour and style TaskKit’s messages, and keep the colour in the printed line so the values TaskKit saves, compares and tests stay plain, even when the test runner turns colours on.
Fundamentals
TypeScript
`chalk`: colors in the terminal. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
You add a task you already have, npm start add "buy milk", and
TaskKit answers with six lines under its banner. One of them says
Already on the list: Buy milk: nothing was added. It looks exactly
like the five lines around it, so your eye slides past it. Now think of
npm test. Its ✔ marks are green and a ✖ is red, and you see a
failure before you read a word.
A highlighter does the same on a printed list: the words stay as they
are, and the one that matters stands out. TaskKit gets its highlighter
in this lesson. By the end, an added task prints green, a skipped one
yellow and an error red, and you know where those colours live, so
they never end up in the values TaskKit saves and tests.
How a terminal shows colour
A terminal shows text. It has no button for "make this green", so a
program asks for colour with text too: a few characters that the
terminal reads as an order instead of printing them. Such a group is
an escape code, because it starts with a character called
escape, ESC for short.
Pressing the Esc key does not put this character into your code (in
VS Code, Visual Studio Code, your editor, it closes things), so code
writes it as \x1B. Inside quotes, a backslash followed by x writes
one character by its number, in hexadecimal (base 16, lesson 12.2),
where 1B is 27. You never need to work it out: \x1B is one
character, ESC.
After it, [32m means "write in green" and [39m means "back to your
normal colour". So the green word done is these characters, one
after another. Press Next step to walk through them, or let it
play:
\x1B[32m
Five characters: an order to write in green. The terminal prints
nothing for them.
done
Four letters, printed as they are, in the colour the order set.
\x1B[39m
Five more: back to your normal colour. Printed as nothing.
chalk.green("done") gives back 14 characters; the terminal
shows 4 of them, in green
All 3 steps shown.Step 1 of 3: \x1B[32mStep 2 of 3: doneStep 3 of 3: \x1B[39m
All 3 steps shown.
The codes are characters in the string, as real as the letters; the
terminal only declines to print them. Nobody wants to remember that 32
is green and 39 is the way back, and chalk is the package that
remembers it for you: you name a colour, and it writes the codes
around your text.
Install chalk by name
chalk passes lesson 14.1's four questions: it colours terminal text,
it has hundreds of millions of downloads a week, its last publish, on
27 September 2026, says it is looked after, and its types are built
in. Its newest version is weeks old, so lesson 14.2's wait is already
over. In the taskkit folder, run:
up to date means npm downloaded nothing. boxen brought chalk 6.0.1
in lesson 9.2, and 6.0.1 is still the newest chalk 6, so npm had
nothing newer to fetch. What changed is one line, in package.json
and in the lock: TaskKit now says that it needs chalk itself. Your
time, in milliseconds (ms), will differ, and so may the version if a
newer chalk 6 is out.
That line is the whole point, because the import works without it:
TypeScript and Node find node_modules/chalk whoever put it there.
Bad — TaskKit imports chalk, and
nothing says it needs it: only boxen does.
Good — after npm install chalk,
TaskKit names chalk, with a range of its own.
With the first one, TaskKit runs on boxen's choice. The day a new
boxen asks for another chalk, or for none, TaskKit gets a chalk you
never checked, or a node_modules with no chalk at all, without a
line of its own code changing.
Colours and styles
chalk is imported the way boxen is, with a name you pick for what the
package hands over (lesson 9.2):
chalk is an object, and each colour is a method on it (lesson 13.1).
chalk.green("done") gives back a new string, the one in the figure
above; the text you passed in stays as it was. A style changes how
the text looks without changing its colour, and chalk has methods for
those too:
Write
The terminal shows
chalk.green("Added")
Green text
chalk.yellow("todo")
Yellow text
chalk.red("Unknown command")
Red text
chalk.dim("not set")
Fainter text, in your normal colour
chalk.bold("Your list")
Bold text
chalk.red.bold("No room")
Red and bold, from one call
The last line chains two names. chalk.red is a method you can call,
and, like chalk, it also carries every colour and style, so
chalk.red.bold(…) writes both codes.
chalk names the colours; your terminal's settings pick the exact shade,
so its green may not be the green on your neighbour's screen.
You never have to learn the list. chalk brought its own description
(lesson 14.1), so when you type chalk. in VS Code, it opens a list of
every name chalk has. A name that is not on it is refused by the
check. There is no orange among a terminal's basic colours:
npm run check stops on it. The first line of the message is one long
line in your terminal, broken here to fit the page (the line and column
are where you typed it, here line 36, inside printSummary):
ChalkInstance is the name chalk's description gives the chalk
object. Pick a colour from VS Code's list instead, such as
chalk.yellow.
chalk colours only for a terminal
TaskKit's text does not always reach a person. A program's output can
be sent into a file instead of the screen, or to another program that
reads it: that is how a computer that runs a team's tests keeps their
reports. You send TaskKit's output into a file yourself in the steps
below. There the codes would be clutter: a file full of \x1B[32m, or
a program that compares Added: Buy milk with something 10 characters
longer. So before it adds a code, chalk looks at standard output,
where console.log writes (lesson 12.3), and asks whether a terminal
is at the end of it.
What runsYour terminalA filenpm start
Colours
Standard output is a terminal, so chalk adds the codes
Plaintext
chalk gives back the words alone: the file has no codes in it
npm test
Colourstoo
The test runner tells chalk its report ends up in your terminal
Plaintext
Nobody will see colours, so chalk adds none
The cell marked Colours too is the one that surprises people.
npm test starts node --test, the test runner: the part of
Node that finds your test files, runs each one as a separate program,
and collects what they report. A test file's output goes to the
runner, not to a terminal. But the runner prints its report to your
terminal in the end, so it tells chalk that colours are welcome.
Inside your tests, then, chalk adds its codes whenever you run them
from your terminal.
TaskKit gets colours
Three kinds of line get a colour: what add says about each task,
the status in the list, and the error TaskKit catches. The values
underneath stay plain. The store keeps saying Added: Buy milk, and
tasks.json keeps "status": "todo".
1
Colour the list in print.ts
Add the import under boxen's, take the Status type from tasks.ts
too (lesson 9.1), and add a small function that picks the colour of a
status:
Then, in printList, replace the two lines inside the loop with these
three, which colour the status and dim the due date:
2
Colour the messages in index.ts
Add import chalk from "chalk"; as the first line of index.ts. In
the add case, look at what the store said before you print it: a
task that went in is green, anything else is yellow.
At the bottom of the file, make the caught error red:
3
Check, run, and test
Your list may hold tasks from earlier runs. Delete tasks.json in
VS Code first, so the list starts empty. Then:
The check prints only its two > lines. Under the banner, TaskKit now
prints:
Add buy milk again, and the first line under the banner is
Already on the list: Buy milk, in yellow. Then mistype a command:
One more run shows the other column of the table. > at the end of a
command sends its output into a file instead of the screen, here a
file named out.txt:
Nothing appears in the terminal. Open out.txt in VS Code. It starts
with npm's two lines, > start and > node index.ts ad (that > is
npm's way of showing what it runs, not the one you typed), then the
banner, then Unknown command: ad. Try: npm start help, with no escape
codes around it, because no terminal was at the end. Delete out.txt
when you have looked.
Run the tests too:
They still end with ℹ pass 14 and ℹ fail 0: every value the tests
compare is as plain as it was.
The words still carry the meaning: todo says todo in any colour, and
in none. That matters for a reader who cannot tell red from green, and
for a file that gets no colours at all. The colour only helps you find
a line faster.
Predict before you run
Here are the two codes from the start of the lesson, written by hand,
this time around a task's name.
Predict
What number does this program print?
The common mistake: colour inside the store
The store writes the messages, so it can seem the natural place to
colour them too.
Common mistake
Returning a coloured message from TaskStore.add
Instead of colouring in index.ts, you import chalk in store.ts and
colour the message where add gives it back:
TaskKit prints the same green line as before. Then you run the tests
in your terminal:
One of the fourteen fails. These lines from its report say which, and
what it got (the time in brackets will differ):
The test from lesson 13.1 compares what add gives back with
"Added: Buy milk". As the table above showed, chalk adds its codes
inside a test you run from your terminal, so add now gives back 25
characters where the test expects 15.
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
The fix
Let add give back the plain message, as it did, and take chalk out
of store.ts. The colour goes where the line is printed, in
index.ts, as in this lesson's step:
\x1B in a failed comparison is an escape code where plain text was
expected. Worse, the same test passes when its output goes into a
file, so it can pass in one place and fail in another. Colour a value
only on its way to the screen, and every value your code compares,
saves or tests stays plain.
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, where chalk is not installed, so two of
them write the codes by hand, the way chalk does. The page is not a
terminal: it prints a code as text, [32m with an invisible ESC in
front, and its check messages write ESC as \u001b, another way to
write the same character.
Recap
Recap · 3 questions
Check yourself
Where should TaskKit colour its Added: message?
Check yourself
You write chalk.orange in print.ts. What happens?
Check yourself
boxen already brought chalk. Why install it by name?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
chalk: colors in the terminal
What things are
Word
What it means
Escape code
Characters a terminal reads as an order, not text
\x1B
ESC, one character, number 27, that starts the code
chalk
A package that writes escape codes around your text
Style
How text looks besides its colour: bold, dim
Standard output
Where TaskKit prints; chalk checks it first
Test runner
node --test: runs each test file, collects reports
Code
Write
What you get
npm install chalk
A line naming chalk in package.json
import chalk from "chalk"
The chalk object
chalk.green(text)
A new string, green in a terminal
chalk.dim(text)
Fainter text, in your normal colour
chalk.red.bold(text)
A colour and a style from one call
\x1B[32m / \x1B[39m
Green, and back to normal
Rules
Rule
What it means
Name what you import
Install it by name, even if it is there
Colour where you print
Values you save, compare and test stay plain
Words first
The text says it; the colour only helps
A terminal, or nothing
No terminal at the end, no codes
npm test in a terminal
Where to go next
You can now say what a coloured word really is, install a package
TaskKit already had so that package.json names it, colour and style
text with chalk, and keep the colours in the printed line, out of the
values TaskKit saves and tests.
Next is commander: a real command-line interface, where TaskKit's
hand-written switch over process.argv gives way to a package that
reads commands and options and writes the help text for you.
Before you move on, open print.ts, type chalk. on a new line inside
printSummary, and read the list VS Code offers. Pick a colour for
the List … full line, run npm start add "walk the dog", and ask
yourself whether the colour helps you find something or only
decorates. Keep it only if it helps.
Where the text ends up
chalk adds its codes only when a person will read the text
in a terminal
Colours too: the runner asks chalk for them
TS2339 on chalk.
No such colour: pick one from VS Code's list
Exercise · Read the code
Up to date, and still a change
You ran npm install chalk in TaskKit, and npm printed up to date, audited 21 packages. Why did it download nothing?
Exercise · Read the code
Fails in the terminal, passes in a file
Someone puts chalk.green inside TaskStore.add. npm test fails
when they run it in their terminal, yet the same tests pass when the
output goes into a file. Why?
Outputtext
up to date, audited 21 packages in 532ms14 packages are looking for funding run `npm fund` for detailsfound 0 vulnerabilities
What npm run check printstext
> check> tscprint.ts:36:21 - error TS2339: Property 'orange' does not exist on type'ChalkInstance'.36 console.log(chalk.orange("late")); ~~~~~~Found 1 error in print.ts:36
What TaskKit printsansi
Added: Buy milkAdded: Call Mom2 tasks added, room for 3 moreList 40% fullYour list, A to Z:#1 [todo] Buy milk, due: not set#2 [todo] Call Mom, due: not set
What TaskKit printsansi
Unknown command: ad. Try: npm start help
Outputtext
✖ add puts a new task on the list (1.071541ms)
Outputtext
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
import boxen from "boxen";import chalk from "chalk";import pluralize from "pluralize";import type { Task } from "./tasks.ts";import type { Status, Task } from "./tasks.ts";import { alphabetical, percentFull } from "./tasks.ts";function paintStatus(status: Status): string { if (status === "done") { return chalk.green(status); } return chalk.yellow(status);}
print.tsts
for (const task of byName) { const status = paintStatus(task.status); const due = chalk.dim(task.due ?? "not set"); console.log(`#${task.id} [${status}] ${task.title}, due: ${due}`); }
index.tsts
for (const title of command.titles) { console.log(store.add(title)); const message = store.add(title); if (message.startsWith("Added:")) { console.log(chalk.green(message)); } else { console.log(chalk.yellow(message)); } }
const green = "\x1B[32m";const normal = "\x1B[39m";const label = green + "Buy milk" + normal;console.log(label.length);
store.tsts
return chalk.green(`Added: ${newTask}`);
bash
npm test
store.tsts
return `Added: ${newTask}`;
index.tsts
console.log(chalk.green(message));
Exercise · Write the code
Paint it by hand
Write the body of paint. It takes some text and a colour's number,
and gives back what chalk would: ESC, [, the number and m, then
the text, then \x1B[39m to go back to the normal colour. 32 is green
and 33 is yellow.
Loading the editor…
Exercise · Fix the error
Colour it where you print it
addedMessage colours its own message, so the first check fails: it
compares the message with plain text, as TaskKit's test does. Make
addedMessage give back plain text, and colour the line where it is
printed instead. The printed line must stay green.