Hello, world: running your first .ts file (and the online Playground)
Write your first TypeScript file in VS Code, save it, and run it with Node from the terminal, from the right folder. Change it and see why an unsaved file prints the old text, write TaskKit's welcome banner, and try code in the online TypeScript Playground.
TypeScript
Tooling
Hello, world: running your first `.ts` file (and the online Playground). Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of the last lesson you had an empty folder called
taskkit, open in VS Code, and Node answering when you asked for
its version. Everything is plugged in. Nothing has made a sound
yet.
Before a concert, someone walks onto the empty stage, taps the
microphone and says "one, two". That is not music. It proves the
whole chain works: the microphone, the cables, the speakers at the
back of the hall. Programmers have a sound check of their own, a
program that prints Hello, world. In this lesson you write it as
the first file in taskkit, run it from the terminal, change it,
and then write TaskKit's welcome banner, the first real piece of
the project. You also meet the Playground, a page where you can
try TypeScript on any computer without installing anything.
A file is a program you keep
In the first lesson your code lived on the page and was gone when
you closed it. On your own computer, code lives in a file: some
text saved under a name, on your computer's disk, its long-term
storage. A file stays there until you delete it, so you can close
everything, come back tomorrow, and run it again.
A file's name ends in an extension: a dot and a few letters
that say what kind of text is inside. list.txt is plain text,
photo.jpg is a picture, and hello.ts is TypeScript. The tools
read the extension before anything else. VS Code sees .ts and
colours the code as TypeScript; Node sees .ts and knows the file
is TypeScript.
Make the file. In VS Code, find TASKKIT in the panel on the
left, the folder you opened in the last lesson. Move the pointer
over it, press the New File... button that appears next to the
name (a page with a plus), type hello.ts and press
Enter (on a Mac the key says return). The
file opens in the editor, empty. Type this
line into it:
hello.tsts
console.log("Hello, world");
You can press Run under the block to see what it prints. The
point of this lesson, though, is the copy on your own computer.
Look at the tab above your code: next to hello.ts there is a
dot, ●. The dot means the file has changes that are only on
your screen. The file on the disk is still empty. Save it with
these keys:
⌘ Cmd+S
Ctrl+S
Ctrl+S
The dot goes away. What you see and what is on the disk are the
same again.
Run it with Node
Open the terminal inside VS Code with the keys from the last
lesson:
⌃ Control+`
Ctrl+`
Ctrl+`
It starts in your project folder, taskkit. Then run the file:
You typebash
node hello.ts
It printstext
Hello, world
That is your first program, run on your own computer. As in the
last lesson, the first word, node, names the program, and the
rest, hello.ts, is handed to it. Node takes that as the name of
a file, opens it, and carries its lines out top to bottom, the way
the page did in the first lesson. The Node you installed runs
.ts files directly: you do not have to turn them into JavaScript
first. The next lesson shows what Node does with a .ts file that
has a mistake in it.
Node does not search your computer for hello.ts. It looks in one
place only: the working directory, the folder the terminal is
standing in. Here is the same command typed twice: first in a
terminal app opened on its own, which starts in your home folder,
then in the project folder. Press Next step to walk through
it, or let it play:
ana — ~/projects/taskkit
ana@mac ~ % nodehello.ts
Error: Cannot find module '/Users/ana/hello.ts'
ana@mac ~ % cd~/projects/taskkit
ana@mac taskkit % nodehello.ts
Hello, world
ana@mac taskkit %
ana@mac ~ % nodehello.ts
Error: Cannot find module '/Users/ana/hello.ts'
ana@mac ~ % cd~/projects/taskkit
ana@mac taskkit % nodehello.ts
Hello, world
ana@mac taskkit %
taskkit
DesktopDesktop
DocumentsDocuments
projectsprojects
taskkittaskkit
hello.tshello.ts
A new terminal opens at home. Node looks for hello.ts here, and
it is not here.
Into the project folder, where VS Code saved the file.
The same command. The file is in this folder now, so Node runs it.
Node looks for the file in the folder the terminal is in
All 3 steps shown.Step 1 of 3: node hello.ts → /Users/anaStep 2 of 3: cd ~/projects/taskkit → /Users/ana/projects/taskkitStep 3 of 3: node hello.ts → /Users/ana/projects/taskkit
All 3 steps shown.
Windows PowerShell
PS C:\Users\ana> nodehello.ts
Error: Cannot find module 'C:\Users\ana\hello.ts'
PS C:\Users\ana> cd~/projects/taskkit
PS C:\Users\ana\projects\taskkit> nodehello.ts
Hello, world
PS C:\Users\ana\projects\taskkit>
PS C:\Users\ana> nodehello.ts
Error: Cannot find module 'C:\Users\ana\hello.ts'
PS C:\Users\ana> cd~/projects/taskkit
PS C:\Users\ana\projects\taskkit> nodehello.ts
Hello, world
PS C:\Users\ana\projects\taskkit>
taskkit
DesktopDesktop
DocumentsDocuments
projectsprojects
taskkittaskkit
hello.tshello.ts
A new terminal opens at home. Node looks for hello.ts here, and
it is not here.
Into the project folder, where VS Code saved the file.
The same command. The file is in this folder now, so Node runs it.
Node looks for the file in the folder the terminal is in
All 3 steps shown.Step 1 of 3: node hello.ts → C:\Users\anaStep 2 of 3: cd ~/projects/taskkit → C:\Users\ana\projects\taskkitStep 3 of 3: node hello.ts → C:\Users\ana\projects\taskkit
All 3 steps shown.
ana@laptop: ~/projects/taskkit
ana@laptop:~$ nodehello.ts
Error: Cannot find module '/home/ana/hello.ts'
ana@laptop:~$ cd~/projects/taskkit
ana@laptop:~/projects/taskkit$ nodehello.ts
Hello, world
ana@laptop:~/projects/taskkit$
ana@laptop:~$ nodehello.ts
Error: Cannot find module '/home/ana/hello.ts'
ana@laptop:~$ cd~/projects/taskkit
ana@laptop:~/projects/taskkit$ nodehello.ts
Hello, world
ana@laptop:~/projects/taskkit$
taskkit
DesktopDesktop
DocumentsDocuments
projectsprojects
taskkittaskkit
hello.tshello.ts
A new terminal opens at home. Node looks for hello.ts here, and
it is not here.
Into the project folder, where VS Code saved the file.
The same command. The file is in this folder now, so Node runs it.
Node looks for the file in the folder the terminal is in
All 3 steps shown.Step 1 of 3: node hello.ts → /home/anaStep 2 of 3: cd ~/projects/taskkit → /home/ana/projects/taskkitStep 3 of 3: node hello.ts → /home/ana/projects/taskkit
All 3 steps shown.
cd ~/projects/taskkit is the cd you know, given a longer path:
~ is your home folder, and each name after a slash is one folder
further down. It takes you to the project from anywhere, in one
command.
Change it, save it, run it again
A program is never written once. You change a line, run it, look at
what it printed, and change it again. Try it: in hello.ts,
replace world with your own name. This course uses Ana.
hello.tsts
console.log("Hello, Ana");
Save, then run node hello.ts once more, and it prints
Hello, Ana.
There is one step in that loop that is easy to skip: the save. Node
never looks at your screen. It reads the file from the disk, so a
change you have not saved does not exist as far as Node is
concerned. Switch between the two versions below to see what
happens. Press Next step to walk through it, or let it play:
VSCode
hello.ts
Node
Screen
VS Code: edit line 1editline1editline1
VS Code: no savenosavenosave
hello.ts: old textoldtextoldtext
Node: node hello.tsnodehello.tsnodehello.ts
Screen: Hello, worldHello,worldHello,world
Time
You change world into Ana. The tab now shows a
dot.
You go straight to the terminal. The dot is still there.
The file on the disk still holds the old line.
Node reads the file from the disk and runs it.
The old text, and no error to say why.
Node reads the file from the disk, not from your screen
All 5 steps shown.Step 1 of 5: VS Code: edit line 1Step 2 of 5: VS Code: no saveStep 3 of 5: hello.ts: old textStep 4 of 5: Node: node hello.tsStep 5 of 5: Screen: Hello, world
All 5 steps shown.
VSCode
hello.ts
Node
Screen
VS Code: edit line 1editline1editline1
VS Code: savesavesave
hello.ts: new textnewtextnewtext
Node: node hello.tsnodehello.tsnodehello.ts
Screen: Hello, AnaHello,AnaHello,Ana
Time
You change world into Ana. The tab now shows a
dot.
You save. The dot goes away.
The file on the disk now holds the new line.
Node reads the file from the disk and runs it.
The new text.
Node reads the file from the disk, not from your screen
All 5 steps shown.Step 1 of 5: VS Code: edit line 1Step 2 of 5: VS Code: saveStep 3 of 5: hello.ts: new textStep 4 of 5: Node: node hello.tsStep 5 of 5: Screen: Hello, Ana
All 5 steps shown.
Nothing warns you when this happens, which is what makes it
costly: you fix a line, run it, see the old result, and start
doubting a fix that was right. Before you run, glance at the tab.
No dot, and the file on the disk is the one you see.
TaskKit's first piece: a welcome banner
hello.ts proved the tools work. Now the project gets its first
real file. Some programs greet you when they start, the way Node's
own prompt said Welcome to Node.js in the last lesson. TaskKit
will open with a banner: its name, its version, and what it is
for.
Make a second file in the same folder, the same way, and call it
index.ts. Index is the name programmers often give the file a
project starts from, so anyone opening taskkit knows where to
begin. Type the banner into it:
index.tsts
const version = "0.1";console.log("============================");console.log("TaskKit", version);console.log("Your tasks, in your terminal");console.log("============================");
The version sits in a box, the way you learned in the first lesson,
because it is the one thing in the banner that will change, and a
box is easier to find and change than a word in the middle of a
line. Save the file and run it:
bash
node index.ts
Outputtext
============================TaskKit 0.1Your tasks, in your terminal============================
taskkit now holds two files: hello.ts, your sound check, and
index.ts, the start of the project. Every module in this level
adds to index.ts until TaskKit is a real task manager.
The Playground: TypeScript without a setup
Sometimes you want to try one idea without opening a project: on a
computer that is not yours, on a phone, or to show a few lines to a
friend. For that there is the TypeScript Playground, a page
made by the TypeScript team at
typescriptlang.org/play. It
runs TypeScript in your browser, and there is nothing to install.
The page has two halves. On the left is an editor, holding a short
welcome program the first time you open it; delete it and type
console.log("Hello, world");. Above the editor is a Run
button. On the right is a panel with tabs; the two you need are
Logs and Errors. If you do not see this panel (on a phone
it starts hidden), press the ⇤ button at the right end of the bar
above the editor. Press Run, and what your code prints appears
under Logs, with each piece of text shown in quotes:
Logstext
[LOG]: "Hello, world"
Errors lists what TypeScript found wrong in your code, and its
tab shows how many there are. The same mistakes are underlined in
red in the editor, as they are in VS Code.
The left end of the bar above the editor, just before Run,
shows which version of TypeScript the page uses, such as v6.0.3.
Version 6 is the one just before the version 7 this course is
tested with, and for everything in this level the two behave the
same.
The Playground keeps only your latest code, and only in this
browser: start something new, or open the page on another
computer, and it is gone. It never saves a file. To keep code,
press Share: it copies a link that holds your code, and opening
the link brings it back anywhere. Here is how the two places you
can now run TypeScript compare:
typescriptlang.org/play
const tasks = 3;console.log(tasks);
The Playground
Does: Runs in the browser, on any computer, with nothing installed
Never: It never saves a file: a link is your copy
% node hello.ts
Hello, world
%
A file on your computer
Does: A file you keep, run by Node from your own terminal
Never: It never runs without Node installed
Try an idea in the Playground; build in a file you keep
typescriptlang.org/play
const tasks = 3;console.log(tasks);
The Playground
Does: Runs in the browser, on any computer, with nothing installed
Does: A file you keep, run by Node from your own terminal
Never: It never runs without Node installed
Try an idea in the Playground; build in a file you keep
typescriptlang.org/play
const tasks = 3;console.log(tasks);
The Playground
Does: Runs in the browser, on any computer, with nothing installed
Never: It never saves a file: a link is your copy
$ node hello.ts
Hello, world
$
A file on your computer
Does: A file you keep, run by Node from your own terminal
Never: It never runs without Node installed
Try an idea in the Playground; build in a file you keep
TaskKit is built in files.
Predict before you run
Earlier in this lesson you saw that Node reads the file from the
disk. Use it.
Predict
The tab shows a dot. What does node hello.ts print?
Your saved hello.ts prints Hello, Ana. You change the name to
Sam and go straight to the terminal. The tab shows its dot:
hello.ts ●ts
console.log("Hello, Sam");
The common mistake: Node cannot find the file
The other surprise every beginner meets is a file Node cannot find,
though it is right there in VS Code. The walk above showed one
cause, the wrong folder. Here is the other.
Common mistake
Running a file whose name has a typo
You are in taskkit, in the right folder, and you type the name a
letter short:
bash
node helo.ts
Node prints more than a dozen lines, most of them about its own
inner workings; the walk above showed only the key one. The one
that matters starts with
Error: Cannot find module: module is Node's word for a file of
code. It ends with the path where Node looked. The folder in that
path is taskkit, the right one, so the file name is what is
wrong.
Run it again with the file's whole name, spelled as ls prints
it:
bash
node hello.ts
Common mistake
Running a file whose name has a typo
You are in taskkit, in the right folder, and you type the name a
letter short:
bash
node helo.ts
Node prints more than a dozen lines, most of them about its own
inner workings; the walk above showed only the key one. The one
that matters starts with
Error: Cannot find module: module is Node's word for a file of
code. It ends with the path where Node looked. The folder in that
path is taskkit, the right one, so the file name is what is
wrong.
Run it again with the file's whole name, spelled as ls prints
it:
bash
node hello.ts
Common mistake
Running a file whose name has a typo
You are in taskkit, in the right folder, and you type the name a
letter short:
bash
node helo.ts
Node prints more than a dozen lines, most of them about its own
inner workings; the walk above showed only the key one. The one
that matters starts with
Error: Cannot find module: module is Node's word for a file of
code. It ends with the path where Node looked. The folder in that
path is taskkit, the right one, so the file name is what is
wrong.
Run it again with the file's whole name, spelled as ls prints
it:
bash
node hello.ts
The path in the error is the clue: it is exactly where Node looked.
If it is the wrong folder, as in the walk above, cd to the right
one. If the folder is right, read the file name in it. A missing
extension gives the same error, as in node hello.
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 the ones about the terminal ask about
it rather than open one. Try any of them in your own terminal too.
Exercise · Read the code
Run the file
You are in taskkit, and ls prints hello.ts. Which command
runs it?
Exercise · Predict the result
Where Node looks
You are in projects, in your home folder, and you type
node hello.ts. Which file does Node try to open? Type its path,
starting from your home folder, ~.
Exercise · Write the code
Version 0.2 of the banner
TaskKit has a new version, 0.2. This banner has the version typed
into its second line, so the box is never used. Change the box to
0.2 and make the banner print it from the box, so the next release
is a one-word change. It should print:
text
============================TaskKit 0.2Your tasks, in your terminal============================
Loading the editor…
Exercise · Fix the error
Save before you run
This program prints the steps for making and running a new file,
but in an order that would print the old text, or nothing at all.
Change the order of the lines, without changing any line itself, so
the steps work from top to bottom.
Loading the editor…
Recap
Recap · 3 questions
Check yourself
You type node hello.ts. Where does Node look for the file?
Check yourself
You changed a line, but Node prints the old text. Why?
Check yourself
What is the TypeScript Playground best for?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Hello, world: running your first .ts file
What things are
Word
What it means
File
Text saved under a name on your computer's disk
Extension
The end of a file name, like .ts: what is inside
.ts
A TypeScript file
● on a tab
Changes on the screen that are not saved yet
index.ts
The file a project starts from
Playground
TypeScript in the browser, at typescriptlang.org/play
Logs
Where the Playground shows what your code printed
Share
Copies a link that holds your Playground code
Commands and keys
Write
To
node hello.ts
Run hello.ts from the folder you are in
cd ~/projects/taskkit
Go to the project from anywhere
Cmd + S
Save the file; Ctrl + S on Windows and Linux
How it runs
Rule
What it means
Save, then run
Node runs the file as it is on the disk
Stand in its folder
Node looks for the file where the terminal is
Read the path
Cannot find module names where Node looked
Try, then keep
Playground for a quick try; a file for the project
Where to go next
You can now make a TypeScript file, save it, run it with Node from
the right folder, and change it until it prints what you want.
TaskKit has its first file and its banner, and you know where to
try an idea when your own computer is not at hand.
Next is Two tools, two jobs: Node runs your code, TypeScript
checks it. You have seen VS Code underline mistakes and Node run
your files; the next lesson takes the two apart, and shows what
each one does with a file that has a mistake in it.
Before you move on, open the Playground, paste in your banner from
index.ts, and run it. Then change the version, save index.ts
on your own computer with the same change, and run it there too.
Two places, one program: notice which one still has your banner
after you try something else in it.