Callsites

4.2.0 · active · verified Tue Apr 21

Callsites is a low-level utility that retrieves an array of V8 CallSite objects from the current JavaScript stack trace. It provides direct access to the V8 stack trace API, allowing developers to inspect intricate details of each call in the stack, such as file names, line numbers, column numbers, function names, and execution context. This package is currently stable at version 4.2.0 and maintains an active release cadence, with recent updates adding Bun support and refining TypeScript definitions. It is a pure ESM package since v4.0.0, making it suitable for modern Node.js environments. Its primary differentiator is providing raw, unopinionated access to V8's callsite information, empowering advanced debugging, logging, and error reporting scenarios.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `callsites` to retrieve the filename of the function that called the immediate parent function in the stack trace.

import callsites from 'callsites';

function getCallerFileName() {
  // callsites()[0] is the call to `callsites` itself
  // callsites()[1] is `getCallerFileName`
  // callsites()[2] is the function that called `getCallerFileName`
  const callerCallsite = callsites()[2];
  if (callerCallsite) {
    return callerCallsite.getFileName();
  }
  return 'Unknown file';
}

function myApplicationLogic() {
  const filename = getCallerFileName();
  console.log(`This function was called from: ${filename}`);
}

myApplicationLogic();
// Example output: This function was called from: /path/to/your/file.js

view raw JSON →