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.
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.
function validate(order) {
console.trace("checking order");
}
function checkout(order) {
validate(order);
}
checkout({ id: 1 });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:1Node.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.