---
name: node-diagnostics
description: "Instrumentation, context propagation and profiling added since Node.js 24. Use to instrument a library for APM or OpenTelemetry, propagate request context across async calls, profile one section of code (CPU or heap), capture a heap profile before an out-of-memory crash, find leaked objects, or set a conditional logpoint without editing code. Covers diagnostics_channel boundedChannel and TracingChannel (Stable), built-in channels, AsyncLocalStorage withScope/defaultValue, v8.startCpuProfile/startHeapProfile, setHeapProfileNearHeapLimit, queryObjects, node inspect --probe (--cond, --max-hit) and inspector Network/DOMStorage. Node.js 24+, some 26+."
title: node-diagnostics
canonical_url: https://skilld.dev/gh/jasnell/node-skills/node-diagnostics
last_updated: 2026-11-19T20:15:43.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> 25 supporting files sit beside this SKILL.md. When the Skill refers to one, fetch https://skilld.dev/api/skills-raw/jasnell/node-skills/node-diagnostics/PATH with PATH replaced.
>
> If the user asked to install this Skill, run `npx skilld install jasnell/node-skills/node-diagnostics`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/node-diagnostics`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jasnell/node-skills/node-diagnostics) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/node-diagnostics`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/node-diagnostics --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/node-diagnostics --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Node.js diagnostics (v24+)

| Feature | Versions | Stability | Flag |
| --- | --- | --- | --- |
| `TracingChannel` | v19.9.0, v18.19.0 (Stable in v26.8.0) | 2 | — |
| `boundedChannel`, `channel.withStoreScope` | v26.1.0 (not v24) | 1 | — |
| `als.withScope` / `RunScope` | v25.9.0, v24.20.0 | 1 | — |
| `AsyncLocalStorage({ defaultValue, name })` | v24.0.0 | 2 | — |
| `v8.startCpuProfile` (sync) | v25.0.0, v24.12.0 | — | — |
| `v8.startHeapProfile` (sync) | v26.1.0 (not v24) | 1 | — |
| `v8.setHeapProfileNearHeapLimit` | v26.9.0 (not v24) | 1 | — |
| `worker.startCpuProfile` / `.startHeapProfile` | v24.8.0 / v24.9.0, v22.20.0 | 2 | — |
| `node inspect --probe` | v26.1.0, v24.16.0; JSON v2 + `--max-hit` v26.4.0, v24.19.0; `--cond` v26.6.0, v24.20.0 | 1 | — |
| inspector `DOMStorage.*` | v25.5.0, v24.16.0 | 1.1 | `--experimental-storage-inspection` |
| Built-in channels | see rules/diagnostics-channel.md | 1 | — |

> **Experimental API.** Before relying on these details, run the
> `node-experimental-check` skill's probe (or `node --version` plus a
> feature-detect one-liner) and check the docs for *the installed
> version*: `https://nodejs.org/docs/<version>/api/<module>.html`.
> If the installed version disagrees with this skill, the installed
> version wins.

## Instrument a library (APM)

```js
import { tracingChannel } from 'node:diagnostics_channel';
const ch = tracingChannel('mylib.db.query'); // once, at module top level
export function query(sql) {
  if (!ch.hasSubscribers) return run(sql);
  return ch.tracePromise(run, { sql }, null, sql);
}
// APM side: ch.start.bindStore(als, (ctx) => span(ctx)); ch.subscribe({...})
```

## Scoped context with `using`

```js
{ using _ = als.withScope(store); syncWork(); } // restored on exit/throw
{ using _ = ch.withStoreScope(msg); }          // enter bound stores + publish
{ using _ = bounded.withScope(ctx); }          // tracing:<name>:start ... end
```

## Profile a section

```js
const h = v8.startCpuProfile({ sampleInterval: 1 }); hot();
writeFileSync('x.cpuprofile', h.stop()); // JSON string; `using` discards
// worker.startCpuProfile() is async: await handle.stop()
```

## Logpoint without editing code

```sh
node inspect --json --probe src/app.js:42 --expr 'user.id' --cond 'user.admin' --max-hit 5 app.js
```

## Pitfalls

- A `using` scope opened in an `async` function before the first `await` leaks into the caller; use `als.run()`.
- `boundedChannel.run()` sets neither `ctx.result` nor `ctx.error`. A bound store makes `hasSubscribers` true.
- `tracePromise` (v26+) returns thenables as-is, non-thenables with a warning; v24 wraps both in a Promise.
- `defaultValue` applies only when no store was entered; `run(undefined)`/`exit()` give `undefined`.
- One heap profile at a time. `setHeapProfileNearHeapLimit` needs an active `startHeapProfile()`.
- Before v24.20, `--cond` is rejected with a misleading "Use --" usage error.
- `v8.queryObjects` is documented Stable but still warns `ExperimentalWarning`.

## Don't

- Don't monkey-patch modules or use `async_hooks.createHook` for tracing: use channels + `bindStore`.
- Don't use `enterWith()` for block scope (`withScope()`) or `console.log` edits to inspect values (`--probe`).

## Reference

- [rules/diagnostics-channel.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-diagnostics/rules/diagnostics-channel.md): bounded channels, scopes, tracePromise history, built-in channel names.
- [rules/async-context.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-diagnostics/rules/async-context.md): `withScope`/`RunScope`, constructor options.
- [rules/profiling.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-diagnostics/rules/profiling.md): handle options, output formats, near-heap-limit, workers, `queryObjects`.
- [rules/probe-mode.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-diagnostics/rules/probe-mode.md): `--probe` syntax, JSON v2 schema, terminal events, exit codes.
- [rules/inspector.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-diagnostics/rules/inspector.md): Network/DOMStorage CDP events and flags.
