1 | # caller-path
|
2 |
|
3 | > Get the path of the caller function
|
4 |
|
5 | ## Install
|
6 |
|
7 | ```sh
|
8 | npm install caller-path
|
9 | ```
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```js
|
14 | // foo.js
|
15 | import callerPath from 'caller-path';
|
16 |
|
17 | export default function foo() {
|
18 | console.log(callerPath());
|
19 | //=> '/Users/sindresorhus/dev/unicorn/bar.js'
|
20 | }
|
21 | ```
|
22 |
|
23 | ```js
|
24 | // bar.js
|
25 | import foo from './foo.js';
|
26 | foo();
|
27 | ```
|
28 |
|
29 | If 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 |
|
35 | Get the path of the caller function.
|
36 |
|
37 | ##### depth
|
38 |
|
39 | Type: `number`\
|
40 | Default: `0`
|
41 |
|
42 | The caller path depth, meaning how many levels we follow back on the stack trace.
|
43 |
|
44 | For example:
|
45 |
|
46 | ```js
|
47 | // foo.js
|
48 | import callerPath from 'caller-path';
|
49 |
|
50 | export 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
|
62 | import foo from './foo.js';
|
63 |
|
64 | export default function bar() {
|
65 | foo();
|
66 | }
|
67 | ```
|
68 |
|
69 | ```js
|
70 | // foobar.js
|
71 | import bar from './bar.js';
|
72 | bar();
|
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>
|