TaskKit gets a backup script and a command you can type in any folder. Learn how a script reports success with its exit code, chain scripts with &&, check scripts before they run, and make TaskKit a command with bin and npm link.
TypeScript
Node.js
Scripts and command-line tools. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Last night your laptop's disk filled up while TaskKit was saving, and
this morning tasks.json holds half a list. You meant to copy it
somewhere every night, and you never once did. And every time a task
comes to mind, you type cd ~/projects/taskkit and then
npm start -- add "Call the bank", as if TaskKit lived in one folder
only.
Both fixes are small programs with no screen: a script that does one
job and ends, and a command you can type in any folder. In this
lesson you write a backup script for TaskKit, make it tell whoever
started it whether it worked, have TypeScript check it before it ever
runs, and give TaskKit a name of its own, taskkit.
A script: one job, then it ends
A script is a short program written for one job, which you run
when the job needs doing and which ends when it is done. TaskKit's
first one copies tasks.json into a backups folder, under a name
with the day in it, so a broken list never costs more than a day. It
lives in a scripts folder of taskkit, apart from the app:
scripts/backup.tsts
import { mkdir, readFile, writeFile } from "node:fs/promises";import { format } from "date-fns";async function backup(day: string): Promise<void> { const text = await readFile("tasks.json", "utf8"); await mkdir("backups", { recursive: true }); await writeFile(`backups/tasks-${day}.json`, text); console.log(`Saved backups/tasks-${day}.json`);}await backup(format(new Date(), "yyyy-MM-dd"));
mkdir makes a folder, and { recursive: true } tells it not to
mind when the folder is already there. backup takes the day as a
parameter, the way lesson 14.5's dueText took the date, so the
same function can run for any day you hand it. This block runs it on
the page. The page's folder starts empty, so the line under the
comment writes a tasks.json for the script to find. Press Run:
scripts/backup.tsts
import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";// Stands in for TaskKit's own tasks.jsonawait writeFile("tasks.json", '[{"title":"Pay rent","due":"2026-10-30"}]');async function backup(day: string): Promise<void> { const text = await readFile("tasks.json", "utf8"); await mkdir("backups", { recursive: true }); await writeFile(`backups/tasks-${day}.json`, text); console.log(`Saved backups/tasks-${day}.json`);}await backup("2026-10-29");console.log(await readdir("backups"));
readdir lists what a folder holds. On your computer, Node runs the
script as it runs TaskKit: it strips the types and runs the rest
(lessons 7.6 and 8.2). Give it a name in package.json, beside
start, check and test, so nobody has to remember its path:
A script runs rarely and nobody watches it, and that is where the
check helps most. tsc, the TypeScript compiler, checks every
.ts file in the folder of its tsconfig.json and the folders under
it (lesson 9.4), unless an include names fewer. Since lesson A1.4,
TaskKit's own tsconfig.json has one that keeps only the *.ts
files beside it, and server and web have checks of their own. So give
scripts one too: copy server/tsconfig.json, which already has
Node's settings, into scripts. Then a
mistake in a script fails the check today, not at midnight a month
from now. Spell recursive wrong and run the check:
bash
npx tsc -p scripts
Outputtext
scripts/backup.ts:7:28 - error TS2769: No overload matches this call. The last overload gave the following error. Object literal may only specify known properties, but 'recursve'does not exist in type 'MakeDirectoryOptions'. Did you mean to write'recursive'?7 await mkdir("backups", { recursve: true }); ~~~~~~~~ node_modules/@types/node/fs/promises.d.ts:708:14 - The lastoverload is declared here. 708 function mkdir(path: PathLike, options?: Mode |MakeDirectoryOptions | null): Promise<string | undefined>; ~~~~~Found 1 error in scripts/backup.ts:7
Long lines are broken here to fit. mkdir has several typed forms,
called overloads (Level 2's lesson Function overloads), and the check
tried each one; the last one names your typo. Node alone would have
run the script with it.
Exit codes: how a script says it worked
Every program that ends hands one number back to whoever started it,
its exit code. 0 means it worked. Any other number means it
failed, and some programs use different numbers for different
failures. Node picks the number for you: it ends with 0 when your
script runs to its last line, and with 1 when an error nobody
catches stops it.
The exit code is what lets one program follow another. Between two
commands, && means "run the second only if the first ended with
0". TaskKit's evening is two scripts, the backup and a short
report of tomorrow's tasks, scripts/report.ts, which reads
tasks.json and prints Due tomorrow: and each title. Its code is
not the point here: any script of your own that prints a line will
do to try it. Its package.json line is called evening:
npm (the Node package manager) hands a script's line to a
shell, the program that reads a command line and runs it: sh
on macOS and Linux, cmd.exe on Windows, and both understand &&. Typed
straight into Windows PowerShell, && is refused; inside an npm script it
works on every system. Press Next step to walk through both evenings, or
let it play, and switch between them:
backup.ts
Exitcode
report.ts
npmrunevening
backup.ts: Saved a copySavedacopySavedacopy
Exit code: 000
report.ts: Due tomorrow: Pay rentDuetomorrow:PayrentDuetomorrow:Payrent
npm run evening: exit code 0exitcode0exitcode0
Time
Node runs the script. It copies tasks.json into backups and
prints the name of the copy.
The script ran to its last line, so Node ends with exit code 0:
it worked.
&& saw 0, so it starts the next command, and the report
prints tomorrow's tasks.
npm ends with the last command's code. Whoever started it, you
or a scheduler, reads 0: the evening worked.
The exit code is all && reads: 0 runs the next command,
anything else stops the chain
All 4 steps shown.Step 1 of 4: backup.ts: Saved a copyStep 2 of 4: Exit code: 0Step 3 of 4: report.ts: Due tomorrow: Pay rentStep 4 of 4: npm run evening: exit code 0
All 4 steps shown.
backup.ts
Exitcode
report.ts
npmrunevening
backup.ts: ENOENTENOENTENOENT
Exit code: 111
report.ts: never startsneverstartsneverstarts
npm run evening: exit code 1exitcode1exitcode1
Time
readFile finds no tasks.json and throws. Nothing catches the
error, so Node prints it and stops the script.
An error ended the script, so Node ends with exit code 1: it
failed.
&& saw 1, so it starts nothing after it. The report never
runs.
npm ends with the code of the command that failed. Whoever
started it reads 1: the evening failed.
The exit code is all && reads: 0 runs the next command,
anything else stops the chain
All 4 steps shown.Step 1 of 4: backup.ts: ENOENTStep 2 of 4: Exit code: 1Step 3 of 4: report.ts: never startsStep 4 of 4: npm run evening: exit code 1
All 4 steps shown.
The terminal keeps the last exit code. On macOS and Linux, echo $?
prints it: echo prints whatever follows it, and $? is where the
terminal keeps the code. Windows PowerShell keeps it in
$LASTEXITCODE, and typing that name alone prints it. Rename
tasks.json to tasks.away.json for a moment (in VS Code's
Explorer: right-click it, Rename) and run the backup alone:
bash
node scripts/backup.ts
Node prints the ENOENT error of lesson 12.2 (short for error, no
entry: no such file), with its stack trace (lesson 1.5), and
stops. Then ask for the code:
bash
echo $?
$LASTEXITCODE
echo $?
Outputtext
1
Rename it back to tasks.json, run the backup again, and the same
question prints 0. With the evening line in package.json,
npm run evening runs both.
Sometimes your script finds the trouble itself, with no error thrown:
an empty list, a folder it may not write to. Then it says so in two
places. console.error prints like console.log, but to the
program's second output, standard error. In the terminal both
look the same; the difference shows when a program or a log keeps
the errors in a place of their own. And
process.exitCode, a property of the process you met in lesson
2.8, is the number Node ends with when the script is done:
ts
console.error("Backup failed: tasks.json is empty");process.exitCode = 1;
Setting it ends nothing: the lines after it still run, and Node ends
with 1 once they have.
A command-line tool: a script with a name
TaskKit already understands commands: lesson 14.4 gave it
commander. What it lacks is a name of its own. A command-line
tool is a program you start by typing its name, as you type node,
npm or tsc, in whatever folder the terminal is in. Lesson 9.2 showed
where tsc comes from: a package lists its commands, and npm puts
them in node_modules/.bin. TaskKit can list one too, in the bin
field of its package.json, which pairs a command's name with the
file it runs:
The package.json of lesson 9.2 has no name, and the command below
needs one: npm names the link after it.
The command runs the file itself, not node with the file, so the
file has to say which program reads it. That is the first line of
index.ts, the shebang, #! and the program to start:
index.ts (first line)ts
#!/usr/bin/env node
/usr/bin/env node means "the node the PATH finds" (lesson 1.2),
wherever it was installed. Node and TypeScript both skip the line.
Windows does not read it; npm does, and writes small files such as
taskkit.cmd and taskkit.ps1 that start Node with your file.
npm link, run in taskkit, turns the bin field into a command for
every folder. It puts a link to your project, an entry that
points at the folder instead of copying it, where npm keeps the
packages installed for the whole computer, and the command in a folder
on your PATH:
bash
npm link
Outputtext
added 1 package, and audited 3 packages in 248msfound 0 vulnerabilities
The numbers depend on TaskKit's packages and your computer.
macOS: npm link says EACCES: permission denied
The folder npm links into belongs to the system, not to you. That happens on a
Mac when Node came from the installer on nodejs.org: npm stops with a long
message that includes EACCES: permission denied, symlink (EACCES is short
for error, access) and the folder /usr/local/lib/node_modules. sudo
(short for superuser do) runs one command with the rights to change system
folders, after asking for your Mac's password. Run sudo npm link, type the
password (it shows nothing as you type), and press return.
So change index.ts, and the next taskkit runs
the change. Lessons A package for npm: build TaskKit, describe it
and try it and Publishing a package to npm show the other way, a
tool other people install, and why it ships JavaScript instead of
.ts files.
Now open a new terminal, which starts in your home folder, and try it
from somewhere that is not the project:
bash
cd ~/Downloadstaskkit add "Call the bank"
TaskKit answers Added: Call the bank and shows a list with that one task in
it, and your list in taskkit never hears of it. store.ts keeps the list in
const file = "tasks.json", and a file name with no folder is looked up in
the working directory, the folder the terminal is in (lesson
12.2). Downloads has no tasks.json,
so load found nothing and started an empty list, and save wrote
a new tasks.json into Downloads.
Bad — keeps the list in whatever
working directory you start it from.
store.ts (part)ts
const file = "tasks.json";
Good — keeps the list beside
store.ts, wherever you start it.
store.ts (part)ts
import { join } from "node:path";const file = join(import.meta.dirname, "tasks.json");
import.meta holds facts about the file that is running, and
import.meta.dirname is the folder it is in, here your project
folder, link or not. join puts the folder and the name together, as
in lesson A1.5. The first version works every time you test it in taskkit,
and quietly splits your list the first time you use it as a tool: a
tasks.json in every folder you happened to start it from.
Predict before you run
Predict
How many lines does console.log print before the run stops?
The message is clear, and nobody reads it: the error was caught, the
script ran to its last line, and Node ends with 0. Ask for the code
after a run with no tasks.json:
In the terminaltext
% node scripts/backup.tsBackup failed: ENOENT: no such file or directory, open 'tasks.json'% echo $?0
PS C:\Users\ana\projects\taskkit> node scripts/backup.tsBackup failed: ENOENT: no such file or directory, open'C:\Users\ana\projects\taskkit\tasks.json'PS C:\Users\ana\projects\taskkit> $LASTEXITCODE0
$ node scripts/backup.tsBackup failed: ENOENT: no such file or directory, open 'tasks.json'$ echo $?0
On Windows the message names the file with its full path, and the line is
broken here to fit.
&& runs the report as if the backup had worked, and so will every
program that starts your script for you: the next lesson's
scheduler, a program that starts commands at set times, and a later
lesson's CI (continuous integration), a check that runs on GitHub
each time someone proposes a change to your code. The
backups stop, and nothing says so.
The same run now prints the same line, and echo $? or
$LASTEXITCODE prints 1.
Every catch in a script that something else starts ends with one
question: will whoever started me know this failed? If the answer is
only a printed line, set process.exitCode.
Pass all three tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
The first one runs on this page: Check runs your code, then its
checks, and shows what TypeScript or a test says.
Exercise · Write the code
Make the backup return its exit code
This backup throws when there is no tasks.json, so the run below
fails before it prints anything. Make it return its exit code
instead: 1 when tasks.json is missing, after printing No tasks.json to back up with console.error, and 0 after saving the
copy. In the real script, its last line would be process.exitCode = await backup(…). The checks test the numbers it returns.
Loading the editor…
Exercise · Read the code
A check before the backup
TaskKit's package.json has
"safe-backup": "npx tsc -p scripts && npm run backup". You run npm run safe-backup, and the check finds a type error in
scripts/backup.ts. What happens?
Exercise · Read the code
Which tasks.json
You change scripts/backup.ts to read
join(import.meta.dirname, "tasks.json") instead of "tasks.json",
so it works from any folder. Which file does it now try to read?
Recap
Recap · 3 questions
Check yourself
What does exit code 0 tell whoever started a script?
Check yourself
What do the bin field and npm link give you?
Check yourself
Why give scripts its own tsconfig.json?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Scripts and command-line tools
What things are
Word
What it means
Script
A short program for one job, run when needed
Exit code
The number a program ends with: 0 worked, else failed
Standard error
A program's second output, for errors
Overload
One of a function's typed forms; the check tries each
Command-line tool
A program you start by its name, in any folder
Shebang
Line 1, #!/usr/bin/env node: who reads the file
Shell
The program that reads a command line and runs it
Code and commands
Write
To
"backup": "node …"
Name a script in package.json
{ recursive: true }
In mkdir: no error if the folder is there
readdir("backups")
List what a folder holds
npx tsc -p scripts
Check the scripts before they run
echo $?
Print the last exit code; macOS, Linux
$LASTEXITCODE
Print the last exit code; Windows
a && b
Run b only if a ended with 0
console.error(…)
Print to standard error
process.exitCode = 1
End with 1 once the script is done
"name", "bin"
Name the package, and its command and file
npm link
Make that command work in every folder
sudo npm link
The same, when the folder belongs to the system
import.meta.dirname
The folder of the running file
How it runs
Rule
What it means
Uncaught error
Node ends with 1
Caught error
Set process.exitCode, or it ends with 0
&& in npm scripts
Works on every system; not in Windows PowerShell
Bare file names
Found in the working directory, not the tool's
Where to go next
You can now write a script that reports whether it worked, read its
exit code and chain scripts with &&, check your scripts before they
run, and give TaskKit a command of its own that keeps its list in one
place.
Next is Automation and serverless functions, programs that
something else starts: your computer's clock runs this lesson's backup
every night, and a platform runs a small function that answers your
friend's phone, and both depend on exit codes and on types, as this
lesson's scripts did.
Before you move on, press Edit on the scripts/backup.ts block
you ran and make backup skip the copy when one for that day is
already in backups, printing Already saved today. Call
backup("2026-10-29") twice, predict what it prints, then run it.