Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

Node.js console.trace() method

Reference for console.trace() in Node.js: printing a message and the current call stack to stderr, stack depth, source maps and ways to capture a stack as text.

console.trace() prints the word Trace: (followed by your message, if any) and the current call stack to standard error. The stack lists the functions that led to the call, newest first, with file names and line numbers.

Use it to find out who called a function in an unexpected way, without throwing an exception or opening a debugger.

NODE
function save() { console.trace('saving'); }

Try it

Node.js Who called me?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Capturing a stack without printing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
console.trace([message][, ...args]): void
Name Description
message Optional first value; a string may contain %s-style specifiers.
...args Further values, formatted like console.log().

Returns: undefined.

Output: Trace: your message and then lines such as at functionName (file:///path/main.mjs:3:20). With no arguments the first line is just Trace.

Usage notes

  • Writes to stderr, like console.error().
  • Depth is limited by Error.stackTraceLimit (default 10 frames). Raise it temporarily, or start Node with --stack-trace-limit=50, to see more of a deep call chain.
  • Asynchronous gaps: after an await, the stack shows frames from the current tick, plus Node's async stack frames (at async fn) when available. It does not show who scheduled a callback in an unrelated tick.
  • Frames from Node itself (node:internal/...) appear at the bottom; they are normal.
  • With --enable-source-maps, paths and lines refer to the original TypeScript or bundled source.
  • To get the stack as a string, use new Error().stack or Error.captureStackTrace(obj), and log or store it as needed.
  • Do not leave it in hot paths. Building a stack costs far more than a normal log call.
  • Inside a group, the lines are indented like any console output.