Two tools, two jobs: Node runs your code, TypeScript checks it
See why node runs a file with a typo halfway and then fails, while TypeScript names the mistake before a single line runs. Install TypeScript into TaskKit with npm, check a file with npx tsc --noEmit, run it with node, and learn why tsc alone is not found.
Tooling
TypeScript
Two tools, two jobs: Node runs your code, TypeScript checks it. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of the last lesson TaskKit printed its banner. Now
imagine you had typed it a little faster, and line 3 came out as
console.log("TaskKit", verison);. You save, run node index.ts,
and the terminal prints one row of = signs, then a block of error
text. Only one line of the banner made it to the screen. But on the
first lesson's pages, TypeScript read every line before any of them
ran, and stopped a file like this before it started. So what
happened?
A newspaper is made by two very different workers. The printing
press prints page after page, typos included, and never reads a
word; when it reaches a page it cannot print, it stops, and the
pages before it are already printed. The proofreader reads every
word and marks every mistake, and never prints a thing. On your
computer, Node is the press and TypeScript is the proofreader, and
so far you have only called the press. By the end of this lesson
TypeScript is installed in taskkit, you check a file before you
run it, and you can tell from a message which of the two wrote it.
Two tools, two jobs
The first lesson drew your code going through two tools: first
TypeScript checks it, then Node runs it. On the lesson pages,
Run does both, one after the other, so you never had to think
about the order. On your own computer they are two separate
programs, started by two separate commands, and each does only its
own job.
Here is what each one does with the banner when line 3 says
verison:
% node index.ts
============================
/Users/ana/projects/taskkit/index.ts:3
console.log("TaskKit", verison);
^
ReferenceError: verison is not defined
Node runs it
Does: Runs the lines in order, until one cannot run
Never: It never looks ahead for a wrong name
% npx tsc --noEmit index.ts
index.ts:3:24 - error TS2552: Cannot find name
'verison'. Did you mean 'version'?
3 console.log("TaskKit", verison);
~~~~~~~
index.ts:1:7 - 'version' is declared here.
1 const version = "0.1";
~~~~~~~
The second command, npx tsc --noEmit index.ts, asks TypeScript to
check a file; you install it and take it apart below. Both found the
same typo, at different moments. Node found it only when it reached
line 3, after line 2 had already printed. TypeScript found it before
anything ran, because reading is all it does. Neither can do the
other's job, so you use both: TypeScript first, then Node. The rest
of this lesson sets that up in taskkit.
Node runs the file as it is
Make the mistake yourself, on purpose, so you know what it looks
like. Change version on line 3 of index.ts into verison, and
save:
VS Code (Visual Studio Code, your code editor) draws a red line
under verison. Ignore it for a minute
and run the file:
Read it from the top. The row of = signs is line 2 of your file,
which ran and printed. Then Node reached line 3. It names the file
and the line, shows the line, and puts a ^ under the place it
could not get past. The last line says what went wrong: a
ReferenceError, JavaScript's word for a name that refers to
nothing; Node prints it. It looked for a box called verison, and
there is none. Several more lines follow, starting with at;
the next lesson reads them. Lines 4 and 5 never ran.
On newer versions of Node, the line that names the file may start
with file:///: the same path, written the way a browser writes
the address of a file.
Node can run a .ts file because it knows which parts of
TypeScript are there only for the checker: the type annotations,
notes about what a box holds, that you start writing in the next
module. It removes those and runs what is left as JavaScript, top
to bottom. Before the first line, it
only makes sure the text has the shape of code, every quote closed
and every bracket matched. Whether a name like verison belongs
to a box, it finds out only when it reaches that line, so a
mistake like this one waits on line 3 until Node arrives.
Install TypeScript into taskkit
The checker is a program of its own, and it does not come with
Node. You get it with npm (Node package manager), the tool from
the setup lesson that installs code other people have written.
TypeScript's checker is exactly that: code written by the TypeScript
team.
Open the terminal inside VS Code:
⌃ Control+`
Ctrl+`
Ctrl+`
It starts in taskkit, which is where TypeScript has to go. Run:
The time, in ms (milliseconds, thousandths of a second), will
differ. npm may add a few lines starting npm notice about a newer
npm; you can ignore them. Word by word: install downloads a
package, a bundle of code with a name, and puts it into this
project. typescript is the package's name. --save-dev writes
it down in the project's list as a dev dependency: a tool you
need while you build TaskKit, not a piece of TaskKit itself. The
two packages are TypeScript and a part of it built for your kind of
computer.
taskkit now holds three new things next to your two files:
taskkit/
node_modules/the installed packages; never edit them
hello.tsyour sound check
index.tsTaskKit
package-lock.jsonthe exact versions npm installed
package.jsonwhat the project uses: typescript
You edit none of the three by hand. A .json file (JSON, JavaScript
Object Notation) holds plain data that tools read. package.json is
the list, package-lock.json pins the exact versions on it, and
node_modules holds the packages themselves. If node_modules is
ever deleted, npm install with no other words reads the list and
puts everything back.
Check that the checker answers, the way you checked Node in the
setup lesson:
Any version from 7 up is fine. tsc, short for TypeScript
compiler, is the checker's command. A compiler turns code into
another form: TypeScript's can also write a JavaScript copy of your
file. npx, a second command that came with Node and npm, starts a
command that was installed into the project you are standing in;
you will see in a few minutes why tsc needs it.
Windows: npx says running scripts is disabled
PowerShell blocks npx for the same reason it can block npm: it
refuses script files until you allow them for your own account.
Run this once, answer Y, and open a new terminal:
Check the file before you run it
index.ts still says verison. Ask TypeScript about it:
In your terminal the first line is one long line; it is broken in
two here to fit the page. TS2552 is the code of this kind of
error: TS for TypeScript, then its number. It says where
(index.ts, line 3, column 24, the 24th character along the line),
that it is an error, and what is wrong, with a guess at
what you meant. Under it, the line itself with ~~~ under the word,
and the line where version was made. The next lesson takes a
message like this apart, piece by piece.
Look at what is missing: there is no row of = signs. Not one line
of your program ran: tsc only reads. --noEmit (to emit is
to write out) means "only check, write nothing". Without it, tsc
also writes a JavaScript copy of your file, index.js, next to it,
even when it found errors, and you do not need one: Node runs .ts
files itself.
The check cannot stop Node either. If you run node index.ts after
a check that found an error, Node runs the file anyway, exactly as
before. Checking first is a habit you keep; nothing forces it.
Now fix line 3 back to version, save, and check again:
It prints nothing at all. As the setup lesson said, no news is good
news: a check that prints nothing found nothing wrong. Then run:
Check, then run
Those two commands, in that order, are the habit this lesson is
for. Switch between the two ways of running the broken banner
below: in Check first you check, fix line 3 and save, and only
then run node index.ts. Press Next step to walk through them,
or let it play:
In a four-line banner, running without a check costs three missing
lines. In a program that saves a file or sends a message before the
broken line, the lines above it have really run, and running it
again does them twice.
You have also seen TypeScript somewhere else: as the last
lesson said, the red line under verison in VS Code is TypeScript
too, run every time you stop typing, and the message you get
when you point at the red line is the same message tsc printed. So
why a command as well? The red line helps only if you are looking at
that file at that moment. npx tsc gives a clear yes or no every time
you ask, before every run, and later in the course one such command
checks every file of the project at once.
Predict before you run
Node runs the lines in order and stops at the first one it cannot
run. Use that.
Predict
How many lines does node tasks.ts print before the error?
You run this file with node tasks.ts, without checking it first:
The common mistake: tsc without npx
Remember how the terminal finds a program: it looks for its name in
the PATH, the list of folders it reads when it opens. Switch between
the two commands below. Press Next step to walk through them, or
let it play:
If npx itself asks Need to install the following packages: tsc@2.0.4 (a package called tsc, version 2.0.4), answer n.
That means you are outside taskkit, npx found no TypeScript
there, and it offers to download an old package that only shares
the name. Run pwd, cd ~/projects/taskkit, and try again.
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 the terminal rather
than open one. Try any of them in your own taskkit too.
Recap
Recap · 3 questions
Check yourself
Why does Node not stop a typo in a name before line 1 runs?
Check yourself
What does --noEmit change in npx tsc --noEmit index.ts?
Check yourself
tsc says command not found, but npx tsc works. Why?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Two tools, two jobs: Node runs, TypeScript checks
What things are
Word
What it means
Node
Runs your code line by line; never checks names first
TypeScript
Checks your code; never runs it
tsc
The command that starts TypeScript's checker
npx
Starts a command installed in this project
--noEmit
Check only; write no index.js
ReferenceError
Node met a name with no box, while running
Package
A bundle of code with a name, installed by npm
node_modules
Where npm puts the project's packages
package.json
The list of what the project uses
Commands, in order
Step
Command
Install, once
npm install --save-dev typescript
Check
npx tsc --noEmit index.ts
Run
node index.ts
Which version
npx tsc --version
How it works
Rule
What it means
Check, then run
Run only after a check that prints nothing
Run alone
Lines run until the first one that fails
Silence is good
A clean check prints nothing at all
npx, not tsc
Where to go next
You can now install TypeScript into a project, check a file with
npx tsc --noEmit before you run it with node, and tell from the
first lines of a message which tool wrote it: error TS and
nothing printed is TypeScript; the first lines of your program's
output, then a ReferenceError, is Node.
Next is Your first error message: how to read it without panic.
You have now seen two messages from two tools; the next lesson
reads them line by line, so that a wall of red text becomes a file,
a line and one sentence you can act on.
Before you move on, break index.ts on purpose three different
ways, one at a time: misspell console, delete a quote, use a box
that does not exist. Each time, predict what npx tsc --noEmit index.ts will say, run it, then fix the line and check again until
it prints nothing.
You
TypeScript
Node
Screen
You: node index.tsnodeindex.tsnodeindex.ts
TypeScript: not askednotaskednotasked
Node: runs lines 1–2runslines1–2runslines1–2
Screen: first linefirstlinefirstline
Node: stops at line 3stopsatline3stopsatline3
Screen: an erroranerroranerror
Time
You run the banner straight after typing it, typo and all.
Nobody asks TypeScript. node runs a file without checking names
first.
Node runs the file from the top. Lines 1 and 2 work.
The first line of the banner is already on the screen.
There is no box called verison, so Node stops right here.
ReferenceError: verison is not defined, under the first line of
the banner.
Lines 4 and 5 never run.
Node meets a mistake when it gets there; TypeScript finds it first
All 6 steps shown.Step 1 of 6: You: node index.tsStep 2 of 6: TypeScript: not askedStep 3 of 6: Node: runs lines 1–2Step 4 of 6: Screen: first lineStep 5 of 6: Node: stops at line 3Step 6 of 6: Screen: an error
All 6 steps shown.
You
TypeScript
Node
Screen
You: npx tscnpxtscnpxtsc
TypeScript: Cannot find name 'verison'.Cannotfindname'verison'.Cannotfindname'verison'.
You: fix, savefix,savefix,save
Node: runs lines 1–5runslines1–5runslines1–5
Screen: the bannerthebannerthebanner
Time
You ask TypeScript to check index.ts before you run it.
It reads all five lines, runs none of them, and names line 3.
You correct line 3, save, and check again. Nothing is printed.
You run node index.ts. Every line works, in order.
All four lines of the banner, and no error.
Node meets a mistake when it gets there; TypeScript finds it first
All 5 steps shown.Step 1 of 5: You: npx tscStep 2 of 5: TypeScript: Cannot find name 'verison'.Step 3 of 5: You: fix, saveStep 4 of 5: Node: runs lines 1–5Step 5 of 5: Screen: the banner
All 5 steps shown.
You
Terminal
npx
TypeScript
You: tsctsctsc
Terminal: find tscfindtscfindtsc
Terminal: not foundnotfoundnotfound
npx: never startsneverstartsneverstarts
Time
You type tsc --noEmit index.ts and press Enter.
The terminal looks for a program called tsc in its PATH.
tsc lives in taskkit/node_modules, which is not on the
PATH. The terminal gives up.
npx was never asked, so nobody looks inside the project.
The terminal looks in the PATH; npx looks in the project
All 4 steps shown.Step 1 of 4: You: tscStep 2 of 4: Terminal: find tscStep 3 of 4: Terminal: not foundStep 4 of 4: npx: never starts
Does: Reads every line and names every mistake it finds
Never: It never runs a single line
The same file with one typo, given to each tool
$ node index.ts
============================
/home/ana/projects/taskkit/index.ts:3
console.log("TaskKit", verison);
^
ReferenceError: verison is not defined
Node runs it
Does: Runs the lines in order, until one cannot run
Never: It never looks ahead for a wrong name
$ npx tsc --noEmit index.ts
index.ts:3:24 - error TS2552: Cannot find name
'verison'. Did you mean 'version'?
3 console.log("TaskKit", verison);
~~~~~~~
index.ts:1:7 - 'version' is declared here.
1 const version = "0.1";
~~~~~~~
Found 1 error in index.ts:3
TypeScript checks it
Does: Reads every line and names every mistake it finds
Never: It never runs a single line
The same file with one typo, given to each tool
Common mistake
Typing the checker's name on its own
You remember that the checker is called tsc, so you type:
bash
tsc --noEmit index.ts
TypeScript is installed. The terminal just does not look where it
is.
zsh: command not found: tsc
The fix
Put npx in front, so the project's own copy is found:
bash
npx tsc --noEmit index.ts
Common mistake
Typing the checker's name on its own
You remember that the checker is called tsc, so you type:
bash
tsc --noEmit index.ts
TypeScript is installed. The terminal just does not look where it
is.
tsc : The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.
The fix
Put npx in front, so the project's own copy is found:
bash
npx tsc --noEmit index.ts
Common mistake
Typing the checker's name on its own
You remember that the checker is called tsc, so you type:
TypeScript is installed. The terminal just does not look where it
is. Do not run the apt install node-typescript line Ubuntu
suggests under the message: it installs an old TypeScript for the
whole computer, not the one in your project.
Command 'tsc' not found, but can be installed with:
The fix
Put npx in front, so the project's own copy is found:
Exercise · Read the code
Who wrote this message
You ran one command in taskkit, and its message, shortened, said
this. Which command was it, and did any line of index.ts run?
text
index.ts:3:24 - error TS2552: Cannot find name'verison'. Did you mean 'version'?Found 1 error in index.ts:3
Exercise · Predict the result
A clean check
You fix the typo, save, and run npx tsc --noEmit index.ts again.
How many lines does it print?
Outputtext
added 2 packages in 403ms
Outputtext
Version 7.0.2
Outputtext
index.ts:3:24 - error TS2552: Cannot find name'verison'. Did you mean 'version'?3 console.log("TaskKit", verison); ~~~~~~~ index.ts:1:7 - 'version' is declared here. 1 const version = "0.1"; ~~~~~~~Found 1 error in index.ts:3
Outputtext
============================TaskKit 0.1Your tasks, in your terminal============================
index.tsts
const version = "0.1";console.log("============================");console.log("TaskKit", verison);console.log("Your tasks, in your terminal");console.log("============================");
bash
node index.ts
bash
npm install --save-dev typescript
bash
npx tsc --version
bash
npx tsc --noEmit index.ts
bash
npx tsc --noEmit index.ts
bash
node index.ts
tasks.tsts
const total = 3;console.log("Tasks for today");console.log("Groceries");console.log("Laundry");console.log("Total:", totl);
Output (the first lines)text
============================/Users/ana/projects/taskkit/index.ts:3console.log("TaskKit", verison); ^ReferenceError: verison is not defined
============================C:\Users\ana\projects\taskkit\index.ts:3console.log("TaskKit", verison); ^ReferenceError: verison is not defined
============================/home/ana/projects/taskkit/index.ts:3console.log("TaskKit", verison); ^ReferenceError: verison is not defined
Exercise · Fix the error
Fix what the checker found
You saved this program as done.ts and checked it with
npx tsc --noEmit done.ts. The check printed:
text
done.ts:3:13 - error TS2552: Cannot find name 'don'. Did you mean 'done'?
Use the message to fix the program, so the check is clean and it
prints its two lines.
Loading the editor…
Exercise · Write the code
Set up the checker, then use it
You are in taskkit, and TypeScript is not installed yet. Write a
program that prints the three commands you run, in order: install
the checker, check index.ts, then run it.