Skip to content
jshacks.dev
Debugging

Print a stack trace with console.trace

Find out who called a function with console.trace, which prints the message and the current call stack to stderr. Node.js 24 and browsers.

1 min read
Summarize in AI

A function runs at a moment you did not expect, and you want to know who called it. console.trace prints a message and the stack at that point.

order.mjsJavaScript
function validate(order) {
  console.trace("checking order");
}

function checkout(order) {
  validate(order);
}

checkout({ id: 1 });
Text
Trace: checking order
    at validate (file:///app/order.mjs:2:11)
    at checkout (file:///app/order.mjs:6:3)
    at file:///app/order.mjs:9:1

Node.js prints a few more frames from its own module loader after these; they are left out here, and the paths will match your machine. Read the list from the top: validate was called by checkout, which was called from the top level of the file. The method is in the console docs.

Use it to find the callers of a function that is called from many places, or where a deprecated function is still used. In a browser, the trace is clickable and jumps to each line.

The stack of an async function starts at the last await, so a trace inside a callback may not show the code that scheduled it.

Share

More snippets

Debugging

Group related logs with console.group

Indent related console output under a label with console.group and console.groupEnd, so interleaved logs from one request stay readable in Node.js 24.

1 min read
Debugging

Inspect an object with console.dir

Show nested objects beyond the default depth of two with console.dir and the depth option, instead of the [Object] placeholders console.log prints.

1 min read
Debugging

Debug a single Vitest test in VS Code

Run one Vitest file under the VS Code debugger with a launch configuration that passes the open file, so breakpoints in tests and source work. Vitest 3.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.