Skip to content

process

process is an ambient global object, available in every module without an import — just like console. It exposes synchronous access to the running process, backed by libc.

console.log("running in: " + process.cwd());
process.setEnv("GREETING", "hi");
console.log(process.getEnv("GREETING")); // "hi"

Environment

process.getEnv(name: string): string // value, or "" if unset
process.hasEnv(name: string): boolean // distinguishes "set but empty" from "unset"
process.setEnv(name: string, value: string): void
if (process.hasEnv("HOME")) {
console.log(process.getEnv("HOME"));
}

Working directory

process.cwd(): string // current working directory
process.chdir(path: string): boolean // change directory; true on success

Command-line arguments & identity

process.argv(): string[] // [program path, ...user args]
process.execPath(): string // absolute path of the running executable
process.platform(): string // "darwin" | "linux"
process.arch(): string // "arm64" | "x64"
let args: string[] = process.argv();
let i: i32 = 1; // argv[0] is the program path
while (i < args.length) {
console.log("arg: " + args[i]);
i = i + 1;
}

Process control

process.pid(): i32 // this process's id
process.ppid(): i32 // parent process id
process.hrtime(): i64 // high-resolution time in nanoseconds
process.exit(code: i32): void // terminate immediately with an exit code

process.hrtime() is a monotonic nanosecond counter; measure elapsed time by subtracting two readings.

if (!process.hasEnv("REQUIRED_TOKEN")) {
console.error("REQUIRED_TOKEN is not set");
process.exit(1);
}