1 | # caller-path
|
2 |
|
3 | > Get the path of the caller function
|
4 |
|
5 | **Important:** You have to use `'use strict';` in your code for this module to work correctly, or make sure the module is an ESM module, which is implicitly strict.
|
6 |
|
7 | ## Install
|
8 |
|
9 | ```
|
10 | $ npm install caller-path
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | // foo.js
|
17 | const callerPath = require('caller-path');
|
18 |
|
19 | module.exports = () => {
|
20 | console.log(callerPath());
|
21 | //=> '/Users/sindresorhus/dev/unicorn/bar.js'
|
22 | }
|
23 | ```
|
24 |
|
25 | ```js
|
26 | // bar.js
|
27 | const foo = require('./foo');
|
28 | foo();
|
29 | ```
|
30 |
|
31 | If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined for some reason, it will return `undefined`.
|
32 |
|
33 | ## API
|
34 |
|
35 | ### callerPath(options?)
|
36 |
|
37 | Get the path of the caller function.
|
38 |
|
39 | ##### depth
|
40 |
|
41 | Type: `number`\
|
42 | Default: `0`
|
43 |
|
44 | The caller path depth, meaning how many levels we follow back on the stack trace.
|
45 |
|
46 | For example:
|
47 |
|
48 | ```js
|
49 | // foo.js
|
50 | const callerPath = require('caller-path');
|
51 |
|
52 | module.exports = () => {
|
53 | console.log(callerPath());
|
54 | //=> '/Users/sindresorhus/dev/unicorn/foobar.js'
|
55 | console.log(callerPath({depth: 1}));
|
56 | //=> '/Users/sindresorhus/dev/unicorn/bar.js'
|
57 | console.log(callerPath({depth: 2}));
|
58 | //=> '/Users/sindresorhus/dev/unicorn/foo.js'
|
59 | }
|
60 | ```
|
61 |
|
62 | ```js
|
63 | // bar.js
|
64 | const foo = require('./foo');
|
65 |
|
66 | module.exports = () => {
|
67 | foo();
|
68 | }
|
69 | ```
|
70 |
|
71 | ```js
|
72 | // foobar.js
|
73 | const bar = require('./bar');
|
74 | bar();
|
75 | ```
|
76 |
|
77 | ---
|
78 |
|
79 | <div align="center">
|
80 | <b>
|
81 | <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>
|
82 | </b>
|
83 | <br>
|
84 | <sub>
|
85 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
86 | </sub>
|
87 | </div>
|