Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

caller-callsite

sindresorhus26.4mMIT6.0.0TypeScript support: included

Get the callsite of the caller function

caller, calling, module, parent, callsites, callsite, stacktrace, stack, trace, function, file

readme

caller-callsite

Get the callsite of the caller function

Install

npm install caller-callsite

Usage

// foo.js
import callerCallsite from 'caller-callsite';

export default function foo() {
    console.log(callerCallsite().getFileName());
    //=> '/Users/sindresorhus/dev/unicorn/bar.js'
}
// bar.js
import foo from './foo.js';
foo();

API

callerCallsite(options?)

Returns a callsite object.

options

Type: object

depth

Type: number\ Default: 0

The callsite depth, meaning how many levels we follow back on the stack trace from the caller.

For example:

// foo.js
import callerCallsite from 'caller-callsite';

export default function foo() {
    console.log(callerCallsite().getFileName());
    //=> '/Users/sindresorhus/dev/unicorn/bar.js'
    console.log(callerCallsite({depth: 1}).getFileName());
    //=> '/Users/sindresorhus/dev/unicorn/foobar.js'
}
// bar.js
import foo from './foo.js';

export default function bar() {
    foo();
}
// foobar.js
import bar from './bar.js';
bar();