You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB

4 years ago
  1. # get-caller-file
  2. [![Build Status](https://travis-ci.org/stefanpenner/get-caller-file.svg?branch=master)](https://travis-ci.org/stefanpenner/get-caller-file)
  3. [![Build status](https://ci.appveyor.com/api/projects/status/ol2q94g1932cy14a/branch/master?svg=true)](https://ci.appveyor.com/project/embercli/get-caller-file/branch/master)
  4. 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.
  5. Inspired by http://stackoverflow.com/questions/13227489
  6. *note: this relies on Node/V8 specific APIs, as such other runtimes may not work*
  7. ## Installation
  8. ```bash
  9. yarn add get-caller-file
  10. ```
  11. ## Usage
  12. Given:
  13. ```js
  14. // ./foo.js
  15. const getCallerFile = require('get-caller-file');
  16. module.exports = function() {
  17. return getCallerFile(); // figures out who called it
  18. };
  19. ```
  20. ```js
  21. // index.js
  22. const foo = require('./foo');
  23. foo() // => /full/path/to/this/file/index.js
  24. ```
  25. ## Options:
  26. * `getCallerFile(position = 2)`: where position is stack frame whos fileName we want.