Coder Social home page Coder Social logo

caller-path's Introduction

caller-path

Get the path of the caller function

Install

npm install caller-path

Usage

// foo.js
import callerPath from 'caller-path';

export default function foo() {
	console.log(callerPath());
	//=> '/Users/sindresorhus/dev/unicorn/bar.js'
}
// bar.js
import foo from './foo.js';
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
import callerPath from 'caller-path';

export default function foo() {
	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
import foo from './foo.js';

export default function bar() {
	foo();
}
// foobar.js
import bar from './bar.js';
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.

caller-path's People

Contributors

aleclarson avatar litomore avatar richienb avatar sindresorhus avatar yaodingyd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

caller-path's Issues

Missing license files

@sindresorhus Hi Sindre, thank you for this package. Would you mind adding the license file to the npm package of v0.1.0 and latest? Without it we're not really able to use it, because it makes the npm package a distribution of proprietary code.

Additionally I noticed the license file does not include the date, which makes it invalid. I'd greatly appreciate it if you could add the dates to the license files in GH repo as well.

Many thanks for your consideration.

Integer as caller depth argument?

Issuehunt badges

Hi @sindresorhus!

Awesome package, I am using it in save-file and in upcoming audio release.

Unfortunately it turns out that oftentimes we need not the caller path, but caller's caller path.
For example, I expect path to be the invocator's path, not the saveAs path:

//test/index.js

const saveAs = require('save-file')

//so I expect the file to be placed into `test` directory
//but caller-path inside `saveAs` returns `saveAs` path `.../node_modules/save-file/index.js`
saveAs(buf, 'my-record.wav')

What I do now is use callsites directly here:

var callerPath = callsites()[1].getFileName()

What I'd suggest is passing an id of the caller for that purpose:

var cpath = callerPath(1)

That would not break the current behaviour and at the same time would allow to specify caller.
If you need a PR I can come up with one.

Thank you for your time, sincerely,


IssueHunt Summary

yaodingyd yaodingyd has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

"Must use import to load ES Module" after updating to v4

Must use import to load ES Module: /home/runner/work/integrify/integrify/node_modules/caller-path/index.js
require() of ES modules is not supported.
require() of /home/runner/work/integrify/integrify/node_modules/caller-path/index.js from /home/runner/work/integrify/integrify/lib/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /home/runner/work/integrify/integrify/node_modules/caller-path/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/runner/work/integrify/integrify/node_modules/caller-path/package.json.

No idea how to fix this.

See: https://github.com/anishkny/integrify/pull/89/checks?check_run_id=3844631811#step:5:100

TypeError: Cannot read property 'getFileName' of undefined

node 4.x

I have /home/user/dev/project/client/jsx/__tests__/require-clean.js

const callerPath = require('caller-path');

function requireClean(smthPath) {
    const calledFrom = callerPath();

    console.log(calledFrom);
    //...
}

module.exports = requireClean;

I use it in /home/user/dev/project/client/jsx/applications/edit/__tests__/application.action.test.js

const requireClean = require('../../../__tests__/require-clean');

describe('Some tests: ', () => {
    beforeEach(function () {
        requireClean('../../../smth');
    });
    //...
});

I must get in console:

/home/user/dev/project/client/jsx/applications/edit/__tests__/application.action.test.js

but I get the error:

TypeError: Cannot read property 'getFileName' of undefined

See caller module. It works correct in the same situation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.