peach a19a732be8 commit message | 2 anni fa | |
---|---|---|
.. | ||
LICENSE.md | 2 anni fa | |
README.md | 2 anni fa | |
index.d.ts | 2 anni fa | |
index.js | 2 anni fa | |
index.js.map | 2 anni fa | |
package.json | 2 anni fa |
This is a utility, which allows a function to figure out from which file it was invoked. It does so by inspecting v8's stack trace at the time it is invoked.
Inspired by http://stackoverflow.com/questions/13227489
note: this relies on Node/V8 specific APIs, as such other runtimes may not work
yarn add get-caller-file
Given:
// ./foo.js
const getCallerFile = require('get-caller-file');
module.exports = function() {
return getCallerFile(); // figures out who called it
};
// index.js
const foo = require('./foo');
foo() // => /full/path/to/this/file/index.js
getCallerFile(position = 2)
: where position is stack frame whos fileName we want.