Restart on every file change
node --watch server.js
Restarts when the entry file or anything it imports changes.
The Node flags and core APIs you reach for every day, from watching a file to reading one, spawning a process, and debugging a hang.
node --watch server.js
Restarts when the entry file or anything it imports changes.
node --env-file=.env server.js
Node 20.6 and up. Vars already in the shell win over the file.
node app.ts
Node 22.18 and 24 strip types by default. On an older 22 pass --experimental-strip-types. Type-only syntax runs; enums and parameter properties need --experimental-transform-types.
node --test
Finds *.test.js files. Write tests with import { test } from "node:test" and import assert from "node:assert/strict".
import { readFile } from "node:fs/promises";
const text = await readFile("notes.md", "utf8");
Leave off "utf8" and you get a Buffer.
const configPath = path.join(import.meta.dirname, "config.json");
Node 20.11 and up. Before that: path.dirname(fileURLToPath(import.meta.url)).
import { helper } from "./helper.js";
ESM imports need the file extension, even when the source file is .ts. Bare ./helper only works under a bundler.
import { execFile } from "node:child_process";
import { promisify } from "node:util";
const { stdout } = await promisify(execFile)("git", ["status", "--short"]);
execFile takes an argument array, so nothing in it goes through a shell.
process.exitCode = 1;
process.exit(1) cuts off pending stdout writes. Setting exitCode lets the event loop drain first.
const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
Throws a TimeoutError after five seconds instead of hanging.
node --inspect-brk server.js
Open chrome://inspect in Chrome. Plain --inspect attaches without pausing.
console.time("query");
await db.query(sql);
console.timeEnd("query");
Prints query: 12.345ms. The label must match on both ends.