Run with Node
node main.js
TypeScript stops at compile time, and Node takes over at run time. Compile the source first, then give the emitted JavaScript file to Node.
tsc main.ts
node main.js
Hello, TypeScript
A silent compile means success. If Node prints the expected line, the edit, compile, and run loop works on this machine.
Engines note
Target Node 16 or newer for every snippet in this chapter. Older runtimes miss APIs used later, such as randomUUID, so pin the requirement early.
Check the local runtime with one command before continuing. The engines text then records the same requirement for teammates.
node --version
v24.22.3
Any version 16 or above is fine. When the printed number is lower, upgrade Node and run the check again.
Modules type note
Node treats project files as CommonJS by default. That default matches the import style used in this course, so no extra setup is needed to start.
Later, when a project needs ESM, one package.json text field switches the mode. Keep the default now and remember the field name type for later.
"type": "commonjs"
The line above is the default mode written as text. It is shown here only as a note, and no config change is required for the next snippet.
Hello argv
Command line words arrive in process.argv, with the first two slots taken by Node and the script path. Slice from index two to keep only user input.
const words: string[] = process.argv.slice(2); const name: string = words[0] ?? "stranger"; console.log("Hello, " + name);
node main.js Ada
Hello, Ada
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does Node run?
Answer
The JavaScript that the compiler emitted
Types exist only at compile time, so Node receives plain JavaScript.
-
Which Node version should this chapter target?
Answer
Node 16 or newer
The chapter uses APIs such as randomUUID, which older runtimes do not have.
-
What are the first two slots of
process.argv?Answer
The Node executable and the script path
User words start at index 2, which is why the examples slice from there.
-
What module mode does Node use by default for a project file?
Answer
CommonJS
A package.json
"type"field switches the mode, and commonjs is the default. -
What does this program print?
const words: string[] = ["Ada"]; const name: string = words[0] ?? "stranger"; console.log("Hello, " + name);Answer
Hello, Ada
The array holds Ada, so the nullish fallback is not used.
-
What does this program print?
const words: string[] = []; const name: string = words[0] ?? "stranger"; console.log("Hello, " + name);Answer
Hello, stranger
The first element is undefined, so the nullish operator returns the fallback.
-
What does this program print?
const args = process.argv.slice(2); console.log(args.length >= 0);Answer
true
The sliced list has some length, and any length is at least zero.
-
What does this program print?
console.log(process.argv.length >= 2);Answer
true
argv always holds the Node path and the script path.
-
What does the compiler say about this program?
console.log(process.argv.slice("2"));Answer
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
slice takes a number index, and the string "2" does not fit it.
-
What does the compiler say about this program?
const name: string = 42; console.log(name);Answer
error TS2322: Type 'number' is not assignable to type 'string'.
A number literal cannot be stored in a variable typed as string.
Article author: Arthur Isaev