UNPKG

1.64 kBMarkdownView Raw
1# caller-path
2
3> Get the path of the caller function
4
5## Install
6
7```sh
8npm install caller-path
9```
10
11## Usage
12
13```js
14// foo.js
15import callerPath from 'caller-path';
16
17export default function foo() {
18 console.log(callerPath());
19 //=> '/Users/sindresorhus/dev/unicorn/bar.js'
20}
21```
22
23```js
24// bar.js
25import foo from './foo.js';
26foo();
27```
28
29If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined for some reason, it will return `undefined`.
30
31## API
32
33### callerPath(options?)
34
35Get the path of the caller function.
36
37##### depth
38
39Type: `number`\
40Default: `0`
41
42The caller path depth, meaning how many levels we follow back on the stack trace.
43
44For example:
45
46```js
47// foo.js
48import callerPath from 'caller-path';
49
50export default function foo() {
51 console.log(callerPath());
52 //=> '/Users/sindresorhus/dev/unicorn/foobar.js'
53 console.log(callerPath({depth: 1}));
54 //=> '/Users/sindresorhus/dev/unicorn/bar.js'
55 console.log(callerPath({depth: 2}));
56 //=> '/Users/sindresorhus/dev/unicorn/foo.js'
57}
58```
59
60```js
61// bar.js
62import foo from './foo.js';
63
64export default function bar() {
65 foo();
66}
67```
68
69```js
70// foobar.js
71import bar from './bar.js';
72bar();
73```
74
75---
76
77<div align="center">
78 <b>
79 <a href="https://tidelift.com/subscription/pkg/npm-caller-path?utm_source=npm-caller-path&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
80 </b>
81 <br>
82 <sub>
83 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
84 </sub>
85</div>