Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Start, build & configure

Start a project and run your first app

TypeScript

Create a Node/TypeScript project with an explicit compiler configuration. Save the complete CLI example below as main.ts before compiling. Commands run in a new empty folder.

Fragment

Requires Node.js and npm. Browser apps use a browser build/entry point; process is Node-specific.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
mkdir hello-cli
cd hello-cli
npm init -y
npm install --save-dev typescript @types/node
cat > tsconfig.json <<'JSON'
{
  "compilerOptions": {
    "target": "ES2023", "module": "NodeNext",
    "strict": true, "types": ["node"], "outDir": "out"
  },
  "include": ["main.ts"]
}
JSON
# Save the CLI example as main.ts, then:
npx tsc -p tsconfig.json
node out/main.js Ada

A complete small CLI app

TypeScript

Read one optional argument and print a greeting. This is a complete Node program, using the setup in “Start a project”.

Complete program

Save as main.ts. node out/main.js Ada prints Hello, Ada! followed by a newline.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const name = process.argv[2] ?? "world";
console.log(`Hello, ${name}!`);

Read and validate an environment setting

TypeScript

Environment values are strings. Reject malformed/out-of-range configuration before starting the application.

Fragment

Node program. Empty, missing and malformed values are distinct; this example defaults only when unset.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const raw = process.env.PORT ?? "3000";
if (!/^[0-9]+$/.test(raw)) throw new Error("Invalid PORT");
const port = Number(raw);
if (!Number.isSafeInteger(port) || port < 1 || port > 65535) {
  throw new Error("PORT must be 1..65535");
}

Parse named command-line options

TypeScript

Node parseArgs checks declared named options and returns a values object. Its default argument input excludes the Node executable/script.

Fragment

Modern Node API. Try --name Ada -v. Choose allowPositionals and strict policies deliberately.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { parseArgs } from "node:util";
const { values } = parseArgs({
  options: {
    name: { type: "string", default: "world" },
    verbose: { type: "boolean", short: "v", default: false }
  }
});
console.log(values.name);

Start another program with an argument list

TypeScript

execFile runs a chosen executable with an argument array, without an intermediate shell by default. Capture its failure and bound its runtime/output.

Fragment

Inside an async function; git must be installed. Avoid enabling shell or composing untrusted text into a shell command.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { execFile } from "node:child_process";
import { promisify } from "node:util";
const execute = promisify(execFile);
const { stdout } = await execute("git", ["--version"], {
  timeout: 5000, maxBuffer: 1024 * 1024
});
console.log(stdout.trim());

Recognize source, build and output

TypeScript

tsc checks/emits JavaScript; --noEmit checks only. --noEmitOnError prevents new output on errors. node out/main.js runs that output. Node with built-in type stripping can run erasable TypeScript directly without type-checking.

Fragment

Shell commands run separately; assumes local TypeScript and Node type-stripping support (Node 22.18+).

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc -p tsconfig.json --noEmitOnError --outDir out
node out/main.js

# Separate direct-source command:
node main.ts

Read command-line arguments

TypeScript

Node process.argv normally begins with the Node executable and script path. Application arguments follow.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const args = process.argv.slice(2);
for (const argument of args) console.log(argument);

Distinguish an unset environment variable from an empty one

TypeScript

Node process.env returns a string or undefined. An existing empty string differs from an unset variable.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const mode = process.env.APP_MODE;
if (mode === undefined) console.log("unset");
else console.log(mode);

Return a process status without skipping required cleanup

TypeScript

Setting process.exitCode lets Node finish normal event-loop work. process.exit terminates synchronously and can cut off pending work.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
process.exitCode = 1;