instrumentation.js

Runs setup code once when the server starts (observability, monitoring).

Since Next 13.4 (stable Next 15) Spec ↗

Syntax

export function register() { ... }

Returns

void | Promise<void> — register() runs on server startup.

Examples

// instrumentation.ts (project root or src/)
export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    await import('./otel')
  }
}
// Capture request errors (Next 15+)
export const onRequestError = async (
  err: unknown,
  request: { path: string }
) => {
  await sendToErrorService(err, request.path)
}

Notes

`register()` is invoked once per server process startup — ideal for initializing OpenTelemetry, APM, or other instrumentation. Guard with `process.env.NEXT_RUNTIME` since it can run in both Node.js and Edge. Next 15 also supports an `onRequestError` export for capturing server errors.

See also