Get the callsite of the caller function
npm install caller-callsite> Get the callsite of the caller function
``sh`
npm install caller-callsite
`js
// foo.js
import callerCallsite from 'caller-callsite';
export default function foo() {
console.log(callerCallsite().getFileName());
//=> '/Users/sindresorhus/dev/unicorn/bar.js'
}
`
`js`
// bar.js
import foo from './foo.js';
foo();
Returns a callsite object.
#### options
Type: object
##### depth
Type: number\0
Default:
The callsite depth, meaning how many levels we follow back on the stack trace from the caller.
For example:
`js
// 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'
}
`
`js
// bar.js
import foo from './foo.js';
export default function bar() {
foo();
}
`
`js``
// foobar.js
import bar from './bar.js';
bar();