npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cross-caller

v1.0.3

Published

Gets the caller function, file and line number in all browsers and server environments

Readme

cross-caller

NPM

Allows a function to get its caller function name, file, line number and position in all JavaScript engines.

This library is useful for debugging, logging, and tracing function calls.

Installation

npm install cross-caller

Usage

// sample.js
import { getCaller } from 'cross-caller';

function main() {
  doSomething();
}

function doSomething() {
  const caller = getCaller(); // Gets the immediate caller
  
  console.log(caller.function); // main
  console.log(caller.file);     // path/to/sample.js
  console.log(caller.line);     // 5
  console.log(caller.position); // 2
}

main();

Features

  • Supports almost all JavaScript engines (browsers, servers, mobile and embedded)
    • V8 (Node.js, Deno, Chrome, Edge, Opera and all other Chromium-based browsers)
    • JavaScriptCore (Safari, Bun and all browsers in iOS)
    • SpiderMonkey (Firefox)
    • Hermes (React Native and Expo)
    • ChakraCore (old Edge and old React Native for Windows)
    • LibJS (Ladybird)
    • V4 (Qt)
    • QuickJS
    • QuickJS-NG
    • Duktape
    • XS
    • Espruino
    • GraalJS
    • Nashorn
    • Jint
    • Rhino (requires an additional step, see below)
    • Internet Explorer 10+ (requires an additional step, see below)
    • njs (requires an additional step, see below)
    • Jurassic (requires an additional step, see below)
    • engine262
    • MuJS
  • Allows getting the caller function name, JS file, line number and position
  • Allows getting the caller up to the 8th depth in any of these JS engines
  • Small, only ~0.3 KB minified and gzipped

Examples

import { getCaller } from 'cross-caller';

function foo() {
  // Gets the immediate caller (which would be bar)
  console.log(getCaller().function === 'bar');

  // Gets the caller of the caller (which would be baz)
  console.log(getCaller(1).function === 'baz');

  // Gets the caller of the caller of the caller (which would be the global scope)
  console.log(getCaller(2));
}

function bar() {
  foo();
}

function baz() {
  bar();
}

baz();
function main() {
  // When there is no caller at this depth, returns null
  console.log(getCaller(2) === null);
}

main();

Caveats

Global Scope, Eval and REPL

Each JS environment has a different way of naming the global scope, eval and REPL. That means that the call site details may vary in each of them.

For instance, in Node.js the global scope is reported as Object.<anonymous>, an eval is reported as eval at X, Y and a REPL is reported as REPL0. In Chrome, the DevTools Console is reported as <anonymous>. In Firefox, an eval is reported as Y line X > eval and the DevTools Console is reported as debugger eval code.

IE 10, IE 11, njs and Jurassic

The stack info is not available until you throw the error. If you really need support in these environments, you can use the workaround below that throws an error to get the stack trace.

However, while this approach does work in all engines, it is not recommended as it can lead to performance issues.

import { parseCallSite } from 'cross-caller';

// Works on IE 10, IE 11, njs and Jurassic
// as well as all other supported JS engines
function getCallerCompat(depth) {
  try {
    throw new Error();
  } catch (err) {
    return parseCallSite(err.stack, (depth || 0) + 2);
  }
}

Rhino

Rhino supports three types of stack trace formats: Rhino's own format, V8 and Mozilla formats. This library does not supports capturing filenames in Rhino's own format, but does on both the V8 and the Mozilla formats.

You can change to either format by setting the system property rhino.stack.style to V8 or MOZILLA, or call RhinoException.setStackStyle(StackStyle.V8) programmatically.

Other

Other JS engines are not supported by this library either because they don't emit stack traces (such as Boa, Kiesel and JerryScript) or they are very old and not widely used anymore (such as IE 9 and older).