Skip to content
jshacks.dev
Node.js

The node --trace-warnings Flag Explained

Find where a Node.js warning comes from with node --trace-warnings in Node.js 24 and 26: stack traces for process warnings, deprecations and where to set it.

8 min read
Summarize in AI
Flat illustration of settings switches, a slider and a gear, with the Node.js, JavaScript, TypeScript and Vite logos.

Sooner or later a Node.js process prints a line you did not write: a deprecation notice, a warning about too many listeners, or something marked experimental. The line names the problem, but not the code that caused it, and the code is usually not yours. It sits in a dependency three levels down, and without a stack trace you are left searching. The node --trace-warnings flag fixes that. It makes Node.js print the stack trace of every process warning, so the first frame outside the runtime points at the call that raised it. This post covers the flag on Node.js 24 and 26: what a process warning is, what the output looks like with and without the flag, how to emit your own warnings with a code and a type, how to read the trace of a deprecation, how to set the flag for a whole environment, how to listen for warnings from code, and when a different flag fits better. The flag is documented on the Node.js command line options page, and the warning API is in the process documentation. Related flags are covered in the --disable-warning flag and the --unhandled-rejections flag.

What a process warning is

Node.js has a channel for messages that are less serious than an error. They are called process warnings. The runtime uses it for deprecations, for experimental features, for a likely memory leak in an event emitter and for a few misuse cases. Your own code and your dependencies can use it too, through process.emitWarning().

A warning has a name that acts as its type, a message, and optionally a code. By default Node.js prints it once to standard error, as one line that starts with the process id. It does not stop the program and it does not change the exit code. That is why warnings are easy to ignore, and why the one that matters is easy to miss in a noisy log.

The default output

Start with a small program that raises a warning.

warn.mjsJavaScript
process.emitWarning("Cache nearly full");
console.log("done");
Shell
node warn.mjs 2>&1 | sed -E 's/^\(node:[0-9]+\) //'
Text
done
Warning: Cache nearly full
(Use `node --trace-warnings ...` to show where the warning was created)

The command merges standard error into standard output and strips the process id from the front of each line, because that number differs on every run. Two things are worth noticing. The warning prints after done, because Node.js emits it on a later tick, after the current code has finished. And the second line is the runtime telling you what this post is about: the warning was printed without its origin, and the flag shows it.

The hint line appears only for the first warning in a process. Later warnings print as one line each.

Turning the trace on

Add the flag and run the same file.

Shell
node --trace-warnings warn.mjs 2>&1 | sed -E 's/^\(node:[0-9]+\) //; s#file://[^ ]*/##' | head -n 2
Text
Warning: Cache nearly full
    at warn.mjs:1:9

The first line is the same warning. The second line is a stack frame: the file, the line and the column of the emitWarning call. The sed expression only shortens the file URL so the sample does not depend on where the file lives, and head keeps the two lines that matter. Without it, Node.js prints the complete stack, including frames inside the runtime that load the module, and those frames differ between Node.js releases.

The hint line is gone, because you already turned the feature on. The only output change is that the stack is included, and that is the point: nothing else about the program moves.

Warnings with a type and a code

process.emitWarning() takes the message, then either an options object or a type and a code. The type becomes the name in the output, and the code is printed in square brackets, which makes the line easy to search for and to filter.

typed.mjsJavaScript
process.emitWarning("Cache nearly full", { type: "CapacityWarning", code: "APP001" });
process.emitWarning("old() is deprecated, use next()", "DeprecationWarning", "APP002");
console.log("done");
Shell
node typed.mjs 2>&1 | sed -E 's/^\(node:[0-9]+\) //'
Text
done
[APP001] CapacityWarning: Cache nearly full
(Use `node --trace-warnings ...` to show where the warning was created)
[APP002] DeprecationWarning: old() is deprecated, use next()

A library should give every warning a stable code. Users cannot match on the message text, which changes between releases, but they can match on a code in a filter, in a log search or with the --disable-warning flag. The type DeprecationWarning is special: Node.js treats it as a deprecation, which means the deprecation flags, --no-deprecation and --trace-deprecation, apply to it as well. The signature is in the process documentation.

Reading a trace in practice

A real case is a deprecation raised by a dependency. The one-line form tells you a function is deprecated and gives a code such as DEP0040, but not who called it. With the trace on, the lines below the warning list the call chain, innermost first. Read it like any other stack:

  1. Skip the frames that begin with node:. They are the runtime.

  2. Find the first frame in a path under node_modules. That is the package that raised the warning.

  3. Find the next frame in your own code. That is the call that led there, and the one you can change.

If the package is the only source, you now know which package to upgrade or replace, and you can search its issue tracker for the code. If the call is yours, the trace gives you the file and the line. Either way the search is a minute long instead of an hour.

A caution about timing: the stack is captured when the warning is created, not when it is printed. For a warning raised from asynchronous code, the trace shows the point where the warning was made, with whatever call stack existed at that moment. If a timer or a callback made the call, the frames above it are the timer machinery, not the code that scheduled it.

Where to set the flag

For a one-off investigation, put it on the command line. For a whole process tree, such as a test run that starts workers, use the NODE_OPTIONS environment variable, which the flag is allowed in.

Shell
NODE_OPTIONS="--trace-warnings" node --test

For a long investigation, a script in package.json keeps it repeatable.

package.jsonJSON
{
  "scripts": {
    "start": "node server.mjs",
    "start:warnings": "node --trace-warnings server.mjs"
  }
}

The environment variable is also the right tool for a hosted service where you cannot edit the start command: set it on the deployment for a day, find the warning, and remove it.

Listening for warnings from code

Warnings are also an event. A handler on process receives each warning as an error object whose name, message and code you can read. This is the way to turn warnings into logs, or into test failures.

listen.mjsJavaScript
process.on("warning", (warning) => {
  console.log(warning.name, warning.code, warning.message);
});

process.emitWarning("Cache nearly full", { type: "CapacityWarning", code: "APP001" });
Text
CapacityWarning APP001 Cache nearly full

The handler receives the warning whether or not the flag is set, and the object always has a stack, so you can log it yourself. The flag changes only what the built-in printer shows. Adding your own listener does not stop the default line from printing, so you will see both unless you also disable the default output with a flag.

A good use is a test setup file that fails the run when a warning appears, so a new deprecation is caught in review instead of in production.

setup-warnings.mjsJavaScript
const seen = [];

process.on("warning", (warning) => {
  seen.push(`${warning.name}: ${warning.message}`);
});

process.on("exit", () => {
  if (seen.length > 0) {
    console.log("unexpected warnings:", seen);
    process.exitCode = 1;
  }
});

process.emitWarning("Config key is deprecated", "DeprecationWarning", "APP003");
Text
unexpected warnings: [ 'DeprecationWarning: Config key is deprecated' ]

The exit handler runs after the event loop has drained, so the warning, which is emitted on a later tick, has been recorded by then. Preload the file in a test run with --import and every warning your tests trigger becomes a failure.

Four other flags touch the same area, and they are easy to mix up.

FlagEffect
--trace-warningsprint the stack trace of every process warning
--trace-deprecationprint the stack trace for deprecation warnings only
--throw-deprecationthrow an error when a deprecation is emitted
--no-warningssilence all process warnings
--disable-warning=<code or type>silence matching warnings only

Use --trace-deprecation when you care only about deprecations and want less output. Use --throw-deprecation to make a deprecation fail loudly in CI, where the throw gives you a stack anyway. Avoid --no-warnings as a habit. It hides the next warning that would have told you something useful, and --disable-warning is the narrower tool when you have decided one specific warning is not your problem.

Limits and mistakes

  • It does not find silent problems. The flag only adds a trace to warnings that are already emitted. A leak that does not trigger a warning shows nothing.

  • It adds output, not information about cause. The trace says where the warning was raised, not why the condition arose. For the listener warning, the trace shows where the extra listener was added, which is the lead you need.

  • Left on in production, it is noisy. Logs fill with multi-line stacks. Use it while diagnosing, then remove it.

  • Frames inside the runtime change between releases. Do not write tests that match the full stack text.

  • A warning printed once is easy to lose. Some warnings, such as the first deprecation of a given code, are printed only the first time. Search for the first occurrence in the log.

  • Hiding is not fixing. Silencing a deprecation moves the failure to the day the feature is removed.

Share

Keep reading

Flat illustration of jobs on a conveyor heading to a rack of workers, with the Node.js, JavaScript, Apache Kafka, Redis and RabbitMQ logos.
Node.js

The Node.js Event Loop Phases Explained

Learn the six phases of the Node.js event loop, where timers, I/O, setImmediate, process.nextTick and promises run, and why their order sometimes surprises you.

6 min read
Flat illustration of jobs on a conveyor heading to a rack of workers, with the Node.js, RabbitMQ, Apache Kafka, Redis and npm logos.
Node.js

What Is Node.js, and What Is It Good At?

Node.js runs JavaScript outside the browser on the V8 engine. Learn what it is, how its event loop works, where it shines, and where another tool fits better.

6 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.