Run with Node
| node main.js | |
| Engines note | |
| Modules type note | |
| Hello argv |
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
v16.20.2
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
Next: npm scripts
Article author: Arthur Isaev