process
JavaScript · Reference cheat sheet
process
JavaScript · Reference cheat sheet
📋 Overview
Node.js exposes the current process as the global process object: environment, CLI args, working directory, exit codes, and stdio streams. Available without an import in most contexts; you can also import process from "node:process".
🔧 Core concepts
process.env: environment variables (strings orundefined).process.argv:[nodePath, scriptPath, ...args].process.cwd(): current working directory (not the script’s folder).process.exit(code?): end process;0success, non-zero failure.stdin/stdout/stderr: readable/writable streams for I/O.- Signals / events:
process.on("SIGINT", ...),beforeExit,uncaughtException(use sparingly).
💡 Examples
import process from "node:process";
// CLI: node app.js --port 9000
const args = process.argv.slice(2);
console.log(process.execPath); // node binary
console.log(process.cwd());
const port = process.env.PORT ?? "9000";
process.stdout.write("hello\n");
console.error("to stderr"); // often process.stderr
// Read stdin (piped input)
async function readStdin() {
const chunks = [];
for await (const chunk of process.stdin) chunks.push(chunk);
return Buffer.concat(chunks).toString("utf8");
}
process.on("SIGINT", () => {
console.log("shutting down…");
process.exit(0);
});
if (!process.env.REQUIRED) {
console.error("REQUIRED is missing");
process.exit(1);
}// Change cwd (affects relative fs paths)
process.chdir("/tmp");⚠️ Pitfalls
process.exit()skips pending I/O andfinallycleanup; prefer letting the loop drain or useexitCode = 1.cwd()is where you launched the process, not__dirname/import.meta.url.argvincludes node and script paths — slice from index2for user args.- Mutating
process.envaffects the current process only (not the parent shell). - Unhandled promise rejections may terminate Node depending on version/flags.
🔗 Related
- dotenv — load
.envintoprocess.env - Path — resolve paths relative to cwd
- Stream — stdin/stdout as streams
- child_process — spawn subprocesses
- Buffer — binary stdin chunks