get-caller-file
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The "get-caller-file" is an essential utility in JavaScript, specifically designed to discern from which file a function has been invoked. This is made possible by examining V8's stack trace at the moment the function is activated. This utility is profoundly inspired by Node/V8 particular APIs and may not function properly on different platforms.
The "get-caller-file" package is simple to use and can be incorporated into your JavaScript code effectively. You initially need to install it using Yarn by running yarn add get-caller-file
in your terminal. Once installed, you can then require the 'get-caller-file' in your script. As an example, in ./foo.js
, you'd write:
const getCallerFile = require('get-caller-file');
module.exports = function() {
return getCallerFile(); // figures out who called it
};
Then in index.js
, you can call the function exported from ./foo.js
:
const foo = require('./foo');
foo() // => /full/path/to/this/file/index.js
The output of foo()
will be the absolute path to the calling file, in this case, index.js
.
You can also pass a position argument to getCallerFile()
to specify the stack frame whose file name you want.
The documentation for the "get-caller-file" package can be found directly on its GitHub repository: https://github.com/stefanpenner/get-caller-file. The README on this page provides a detailed explanation of the utility's functionality and usage.