Sub topic 3
Writing the Entry Point
This chapter assumes you know basic TypeScript, functions, async/await, and how to run Node.js commands in a terminal. You also need a TypeScript project with a tsconfig.json that compiles src into dist.
An entry point is the file where a program starts. For this command-line tool, use src/cli.ts. The compiler turns it into dist/cli.js, and Node.js runs that JavaScript file.
Add a shebang
A shebang is the first line of an executable script. It tells an operating system which program should run the file.
#!/usr/bin/env node
console.log("The command-line app is running.");TypeScript preserves this line when it compiles the file. After compilation, the generated JavaScript can run directly on systems that support executable files:
node dist/cli.jsThe shebang also allows this form after the file has executable permission:
./dist/cli.jsOn Windows, the node command is still the usual way to run the compiled file.
Keep startup code small
The entry point should coordinate the program rather than contain every detail. A useful pattern is:
- Read the command-line arguments.
- Pass them to a
mainfunction. - Return an exit code from
main. - Assign that code to
process.exitCode.
process.argv is an array containing the values passed to Node.js. The first two entries describe the Node executable and script path, so application arguments begin at index 2.
#!/usr/bin/env node
async function main(argv: string[]): Promise<number> {
const [name] = argv;
if (!name) {
console.error("usage: hello <name>");
return 2;
}
console.log(`Hello, ${name}!`);
return 0;
}
process.exitCode = await main(process.argv.slice(2));The slice(2) call removes Node.js and script details. The main function receives only arguments that belong to the application.
An exit code is a number that tells the calling shell whether the command succeeded. Conventionally, 0 means success. A nonzero value means failure. In this example, 2 means the user supplied invalid input.
Why return an exit code?
You could call process.exit(2) immediately when input is missing. Returning a code is usually easier to test and compose. It also lets pending output finish before Node.js exits.
Use console.log for normal output. Use console.error for errors and usage messages. These methods write to different streams: standard output (stdout) and standard error (stderr).
The entry-point flow
This structure gives later chapters a stable place to add argument parsing, file reading, and error handling. The entry point can remain a thin adapter between Node.js and the application logic.
Run the entry point
Compile the project and run the generated file:
npx tsc
node dist/cli.js Ada
node dist/cli.jsThe first command prints:
Hello, Ada!The second prints the usage message and exits with code 2. You can inspect the code in a POSIX-compatible shell:
node dist/cli.js
echo $?Common mistakes
- Reading
process.argv[0]as the first user argument. - Forgetting to compile TypeScript before running
dist/cli.js. - Calling
process.exit()before output has been flushed. - Mixing argument parsing, file operations, and business logic in one large function.
- Sending errors to
stdoutinstead ofstderr.
Exercise
What does process.argv.slice(2) provide to main?
Show answer
It provides only the command-line arguments supplied by the user, excluding the Node.js executable path and the script path.