Your first input: command-line arguments with process.argv
Give a program its input on the line that starts it. See how the terminal splits the line, what Node puts in process.argv, and read each word by its index from 0. Keep words together with quotes, teach TypeScript about Node with @types/node, turn typed digits into a number, read TS2363, and make TaskKit read a command and a task.
Fundamentals
TypeScript
Your first input: command-line arguments with `process.argv`. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
In the last lesson you renamed TaskKit's line to divider and
wrote down why its percent is rounded. Now add a task to it, say
call mum. You open index.ts, copy the lines that add " buy milk ", change the text in the copy, save, check and run. Tomorrow, for
the next task, you open the file again. Every task means editing the
program.
Think of a taxi. The car is the same on every ride; you do not
rebuild it to go somewhere new. You get in and say the address,
and the driver takes it from there. A program can work the same
way: you write it once, and each time you start it you say what
you want this time, on the same line that starts it. By the end
of this lesson you will type node index.ts add "buy milk" in the
terminal, and TaskKit will read the command and the task from that
line, tidy the task, and print both, without a single edit to
index.ts.
Words after the file name
Since your first program in lesson 1.3 you have started Node with
two words: node, the program to start, and the file it should run.
The terminal reads the first word, finds the program, starts it and
passes it the rest of the line. So far the rest of the line has been
only the file name.
You can type more after it. Each extra piece the terminal splits off
is a command-line argument: a value you give a program on the
line that starts it. An argument here is the same idea as in Module
1, where 28 was the argument you gave repeat(): the value you
actually put in. The program decides what to do with it, and the
same program can do something different on every run, because the
words are different.
Node keeps those words for your program in a list before your
first line runs. Press Next to follow one line from the
keyboard to your code, or let it play:
node index.ts add "Buy milk" ⏎
You type the line and press Enter. To the terminal it is still
one line of text.
node · index.ts · add · Buy milk
The terminal splits it at spaces. The quotes keep Buy milk
together, and are dropped.
process.argv
Node fills a list before your first line runs: its own path,
your file's path, "add", "Buy milk".
process.argv[3] → "Buy milk"
Your code reads one item by its index, counted from 0, and
keeps it in a box.
From the line you type to one word your code can read
All 4 steps shown.Step 1 of 4: node index.ts add "Buy milk" ⏎Step 2 of 4: node · index.ts · add · Buy milkStep 3 of 4: process.argvStep 4 of 4: process.argv[3] → "Buy milk"
All 4 steps shown.
The terminal does the splitting, not Node and not your code: the
setup lesson warned that cd My Projects reads as two words for
exactly this reason. Node only collects what the terminal hands
it, and your code only reads what Node collected.
Node's list: process.argv
Math is a name that comes with the language, as lesson 2.5 showed.
process is a name that comes with Node: it stands for the
program that is running right now, yours. Among the facts it carries
is the property process.argv, the list of words from the
command line. The name is short for argument vector, an old word
for a list of arguments.
See it for yourself. In ~/projects/taskkit, make a new file
args.ts with one line:
Run it with two words after the file name, the second one in
quotes:
That is how Node prints a list: square brackets around the
items, a comma between them, each string in single quotes. Module 4
calls it an array and is all about them; for now, read this one
top to bottom.
The first item is where the node program is installed on your
computer, and the second is the full path of the file it runs.
Yours will differ with your system and the way you installed Node.
On Windows, Node prints each backslash twice: that is how JavaScript
writes one inside quotes.
The words you typed come only after those two, and the quotes
around Buy milk are gone: they told the terminal to keep two words
together, and then their job was done.
TypeScript has to be told about Node
You have run args.ts, but you have not checked it, and every
lesson since Module 1 has checked before it ran. Check it the usual
way, with npx tsc --noEmit args.ts (tsc is short for TypeScript
compiler, and npx starts the copy in taskkit), and it is
refused with TS2591: the first card below.
Node ran the file happily, because process is Node's own. But
TypeScript knows only the language: Math, strings, numbers. It
has never heard of what Node adds to it, so to TypeScript
process is a name nobody made.
The fix is a package that describes Node to TypeScript, called
@types/node. It holds Node's type definitions, the words
the message used: no code to run, only a description of which names
Node provides and what type each one has, such as process.argv
being a list of strings. Install it with npm (Node package
manager) the way you installed TypeScript, as a dev dependency. The
@24 asks for the version that describes Node 24, the one you run.
TypeScript does not load installed descriptions on its own, so check
with one more flag, --types node, which names the one to load:
The two commands to type:
Silence means the check passed, as always. You install the package
once; package.json now lists @types/node next to typescript.
The flag, though, goes on every check from now on, so the check
command for TaskKit is npx tsc --noEmit--types node index.ts. The
message's first line also ended with Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig. (npm i is short for npm install.) A tsconfig is a
settings file that would remember the flag for you, and Module 9
sets one up and comes back to @types packages and the types
setting. Until then, the flag does the same job.
Reading one word by its position
Each item in the list has an index, its place counted from 0,
exactly as the characters of a string had in lesson 2.4. To read one
item, write the list's name and the index in square brackets:
process.argv[2] is the item at index 2. Index 0 is Node's path and
index 1 is your file, so the first word you typed is always at index
2.
Replace the line in args.ts with these four, so each word gets a
box with a name that says what it holds:
Every item in process.argv is a string, whatever you typed; the
next section shows what that means for numbers. VS Code (Visual
Studio Code, your editor) does not know about --types node, so
until Module 9's settings file it keeps a red line under process,
and its hover says any, TypeScript's word for "I don't know the
type". That red line is VS Code's, not your check's: the terminal
check is the judge. To see it, change the first line of args.ts to
const command: number = process.argv[2]; and run npx tsc --noEmit--types node args.ts. It refuses the line with Type 'string' is not assignable to type 'number'.; then change the line back.
The quotes decide how many words the terminal sees, and nothing
complains when they are missing.
Bad — no quotes, so the terminal
splits the task into two words and index 3 holds only the first.
Good — in quotes, the task is
one argument to the terminal and one item in the list.
The first version gives no error at all: "milk" sits unread at
index 4, and TaskKit would add a task called Buy. Whenever an
argument holds a space, put it in quotes.
Every word arrives as a string
The terminal has no idea what your words mean, so it hands over
text. Type 15 and your program receives "15": two characters, a
string, exactly like the "0" in lesson 2.2. Lesson 2.5 promised
this moment, and gave you the tool for it, Number().
Say you want a small program that adds a five-minute break to an
estimate you type. Press Next to see the two boxes it
makes:
const minutes: number holds 15constminutes15number
Typed as 15, read as the text 15; Number() makes it a number
All 2 variables shown.Variable 1 of 2: const typedMinutes: string holds "15"Variable 2 of 2: const minutes: number holds 15
All 2 variables shown.
Only the second box is safe to count with. As code, in a new file
estimate.ts:
Leave Number() out and write typedMinutes + 5, and TypeScript
lets it through, because + between a string and a number is
allowed: it glues, as lesson 2.3 showed. The program prints With a break: 155. Type a word instead of digits, node estimate.ts ten,
and minutes is lesson 2.5's NaN (not a number). A program
cannot refuse bad input until Module 3 teaches it to decide; for
now, turn the text into a number right after you read it, before any
arithmetic.
TaskKit reads a command and a task
Now TaskKit's step for this module. The task it adds comes from
the command line instead of the code, together with a command
that says what to do with it. Open index.ts in
~/projects/taskkit. Delete the two blocks that add " buy milk "
and "call the dentist ", marked -, and put the lines marked
+ in their place:
The tidying is lesson 2.4's, unchanged: trim the spaces, then a
capital first letter. It matters more now, because a person types
the task and people type however they like. Check the file with the
new flag, then run it with a command and a task:
Run it again with node index.ts add " call the dentist " and
TaskKit prints Task: Call the dentist, without one edit to the
file. It repeats the command but does not act on it yet: type
remove instead of add and it still counts the task as added.
Choosing what to do by the command is the first lesson of Module
3.
Predict how many words Node hands over
length told you how many characters a string has. On a list it
tells you how many items it holds. Read the command, count the way
Node counts, and decide what this prints:
Predict
You run the command below. What does the program print?
The common mistake: counting with typed text
You know the estimate arrives as a string, but when you write the
arithmetic, the box looks like a number to you: you typed 15.
Common mistake
Subtracting from text read off the command line
You want to know how much of the hour is left after a task, so
you read the estimate and subtract it from 60:
npx tsc --noEmit--types node minutes.ts refuses line 2 with
TS2363, TypeScript's code for this error, before anything runs. -
only works on numbers, and typedMinutes is a string.
The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
The fix
Turn the text into a number on the line that reads it:
node minutes.ts 15 prints 45 minutes left in the hour.
When TS2363 (or its left-hand twin TS2362, from Module 1's `"="
28) points at a value from process.argv, wrap the read in Number(). +` gives no such warning: it glues text, so convert
every typed number before you count with it.
Pass all five tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
The page cannot type a command line for you, so where a program
would read process.argv, these exercises put the value it would
hold straight into a box, with a comment saying where it came
from. Use your own terminal to try any of them for real.
Recap
Recap · 3 questions
Check yourself
You run node index.ts add milk. Where is add?
Check yourself
You run node estimate.ts 15. What is in process.argv[2]?
Check yourself
npx tsc: Cannot find name 'process'. What is missing?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Your first input: command-line arguments
What things are
Word
What it means
Command-line argument
A piece of the line after the file name
process
The running program, a name that comes with Node
process.argv
The list of words from the command line
Index
An item's place in the list, counted from 0
@types/node
A package that describes Node to TypeScript
--types node
The check flag that loads those descriptions
Code
Write
To
node index.ts add "…"
Run TaskKit with a command and a task
process.argv[2]
Read the first word you typed
process.argv.length
Count the items, the two paths included
Number(process.argv[2])
Read a typed number as a number
@types/node@24
Teach TypeScript about Node: install once with npm
Rules
Rule
What it means
Two paths first
Index 0 is Node, index 1 your file
Quotes keep words
"Buy milk" is one item; Buy milk is two
Always a string
15 arrives as "15"
Number() first
Convert before you count with it
+ glues
"15" + 5 is "155", with no error
Module 2 on one page
Cheat sheet
Module 2: Values and Variables
const and let
Write or rule
What it means
const version = "0.1";
Make a locked box, filled on the same line
let added = 0;
Make a box you will refill
added = added + 1;
Refill it: the right side is worked out first
const first
Switch to let only when a line refills the box
Basic types: string, number, boolean
Write or rule
What it means
string
Text between quotes, even if it is all digits
number
A count or an amount, whole or decimal, no quotes
boolean
true or false, and nothing else
let added: number = 0;
A box for numbers, typed with a colon
One box, one type
let changes the value, never the type
Arithmetic, comparisons, and template strings
Write or rule
What it means
limit - added
Work out a number: also +, *, /, %
added === limit
Ask whether two values are equal
`${done} of ${total}`
Build text with values inside it
Text joins
"Tasks: " + 2 is "Tasks: 2", never a sum
Working with strings: common methods
Where to go next
You can now give a program its input on the line that starts it,
read each word by its index from process.argv, keep two words
together with quotes, and turn typed digits into a number before
you count with them. TypeScript knows about Node, and TaskKit
reads a command and a task from the terminal. That closes Module
2: you can name values, give them types, work with numbers and
text, and take them in from outside.
Next is if / else and switch, the first lesson of
Module 3. TaskKit reads its command but treats every one the
same; there it learns to choose what to do by the command, and to
say something helpful when a word is missing instead of stopping.
Before you move on, run TaskKit three times with tasks of your
own, one of them with spaces you did not mean to type, and watch
the tidying clean them up. Then run node index.ts add with no
task, read the TypeError with the five questions from Module 1,
and find the line it names.
TS2591
No Node types: install @types/node, add --types node
TS2362, TS2363
Arithmetic on a string: wrap it in Number()
Missing word
The item is undefined, and trim() stops the run
Write or rule
What it means
task.trim()
Remove the spaces and tabs at both ends
task.slice(0, 3)
Cut indexes 0 to 2 out of it
task.includes("milk")
Ask whether "milk" is in it
task.length
Count its characters
Keep the answer
A method gives back a new string; keep it in a box
Number pitfalls: NaN, floating point, and Math
Write or rule
What it means
Number("15")
Turn text into a number: 15
Number.isNaN(minutes)
Ask whether a value is NaN
Math.round(2.5)
Go to the nearest whole number: 3
Round first
Round before you show or compare a decimal
Inference: when TypeScript knows the type
Write or rule
What it means
let added = 0;
Hover shows let added: number
const limit = 5;
Hover shows const limit: 5
The right side decides
No type written: the first value gives it
Write it
When the first value is not the whole story
Comments and names: writing code other people can read
Write or rule
What it means
Say what it holds
percent, not p; the unit when there is one
camelCase
firstTask: words joined, a capital for each new one
Capitals count
taskName and taskname are two names
F2
Rename the name under the cursor everywhere
Why, not what
A comment says what the code cannot
Your first input: command-line arguments
Write or rule
What it means
process.argv[2]
The first word typed after the file name
Quotes keep words
"Buy milk" is one item; Buy milk is two
Always a string
Wrap a typed number in Number()
--types node
On every check, so TypeScript knows process
% npx tsc --noEmit args.ts
args.ts:1:13 - error TS2591: Cannot find name
'process'. Do you need to install type definitions
for node?
1 console.log(process.argv);
~~~~~~~
Found 1 error in args.ts:1
Before: TypeScript knows only the language
Never: Knows what Node adds, such as process
% npm install --save-dev @types/node@24
added 2 packages, and audited 5 packages in 1s
found 0 vulnerabilities
% npx tsc --noEmit --types node args.ts
%
After: Node's types installed and loaded
Does: Checks process.argv like any other name
The same file, checked before and after Node's types are installed
'process'. Do you need to install type definitions
for node?
1 console.log(process.argv);
~~~~~~~
Found 1 error in args.ts:1
Before: TypeScript knows only the language
Never: Knows what Node adds, such as process
$ npm install --save-dev @types/node@24
added 2 packages, and audited 5 packages in 1s
found 0 vulnerabilities
$ npx tsc --noEmit --types node args.ts
$
After: Node's types installed and loaded
Does:
Exercise · Predict the result
Without quotes
You run this command. What does process.argv[3] hold?
bash
node args.ts add Buy milk
Exercise · Read the code
What is at index 0
You run node index.ts add "Buy milk". What does
process.argv[0] hold?
Exercise · Read the code
Keep the task in one piece
Which command makes process.argv[3] hold Call the dentist?
Outputtext
Command: addTask: Buy milk
Outputtext
Command: addTask: Buy
Outputtext
Command: addTask: Buy milk
Outputtext
With a break: 20
Outputtext
============================TaskKit 0.1Your tasks, in your terminal============================Command: addTask: Buy milkTasks added: 1 of 5, room for 4 moreList 20% full
TaskKit has added 2 tasks, and a person types how many more to
count. This program should print Total: 5, but it prints
Total: 23. Fix it so the typed count is added as a number.
Loading the editor…
Exercise · Write the code
Read the command line into boxes
A person ran node index.ts add " call mum " today. The list below
is what process.argv would hold. Make command and typedTask
hold the items at the right indexes, then print the command and the
tidied task as one line, exactly add: Call mum. The list is
written the way Node printed it: square brackets, a comma between
items.
Loading the editor…
PS C:\Users\ana\projects\taskkit>PS …\taskkit>
After: Node's types installed and loaded
Does: Checks process.argv like any other name
The same file, checked before and after Node's types are installed
Checks process.argv like any other name
The same file, checked before and after Node's types are installed