JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6790771
  • Score
    100M100P100Q211118F
  • License MIT

Get the path of the caller function

Package Exports

  • caller-path

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (caller-path) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

caller-path

Get the path of the caller function

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.

Install

$ npm install caller-path

Usage

// foo.js
const callerPath = require('caller-path');

module.exports = () => {
    console.log(callerPath());
    //=> '/Users/sindresorhus/dev/unicorn/bar.js'
}
// bar.js
const foo = require('./foo');
foo();

If the caller's callsite object getFileName was not defined for some reason, it will return undefined.

API

callerPath(options?)

Get the path of the caller function.

depth

Type: number
Default: 0

The caller path depth, meaning how many levels we follow back on the stack trace.

For example:

// foo.js
const callerPath = require('caller-path');

module.exports = () => {
    console.log(callerPath());
    //=> '/Users/sindresorhus/dev/unicorn/foobar.js'
    console.log(callerPath({depth: 1}));
    //=> '/Users/sindresorhus/dev/unicorn/bar.js'
    console.log(callerPath({depth: 2}));
    //=> '/Users/sindresorhus/dev/unicorn/foo.js'
}
// bar.js
const foo = require('./foo');

module.exports = () => {
    foo();
}
// foobar.js
const bar = require('./bar');
bar();

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.