Nodejs: get filename of caller function

node callsite
javascript get caller information
caller of the function javascript
node js get name of calling function
javascript get caller context
nodejs get current line
typescript caller
function prototype caller

I wonder how-to get an absolute path of a caller of a function?

Let say that:

in file a.js I call b(); b() is a function defined in file b.js. a.jsrequires b . So how do I get a.js absolute path from b.js in node?

This is an example how to use stacktrace to find caller file in node

function _getCallerFile() {
    try {
        var err = new Error();
        var callerfile;
        var currentfile;

        Error.prepareStackTrace = function (err, stack) { return stack; };

        currentfile = err.stack.shift().getFileName();

        while (err.stack.length) {
            callerfile = err.stack.shift().getFileName();

            if(currentfile !== callerfile) return callerfile;
        }
    } catch (err) {}
    return undefined;
}

get-caller-file, [Build Status](https://travis-ci.org/stefanpenner/get-caller-file.svg? This is a utility, which allows a function to figure out from which file it was getCallerFile( position = 2) : where position is stack frame whos fileName we want. How can one get the name and line of a function that called the current one? I would like to have a rudimentary debugging function like this (with npmlog defining log.debug): function debug() { var callee, line; /* MAGIC */ log.debug(callee + ":" + line, arguments) } When called from another function it would be something like this:

Failing to restore the prepareStackTrace function can cause issues. Here's an example that removes side-effects

function _getCallerFile() {
    var originalFunc = Error.prepareStackTrace;

    var callerfile;
    try {
        var err = new Error();
        var currentfile;

        Error.prepareStackTrace = function (err, stack) { return stack; };

        currentfile = err.stack.shift().getFileName();

        while (err.stack.length) {
            callerfile = err.stack.shift().getFileName();

            if(currentfile !== callerfile) break;
        }
    } catch (e) {}

    Error.prepareStackTrace = originalFunc; 

    return callerfile;
}

Function.caller, Nodejs: get filename of caller function, I need to get the file path of the caller function dynamically, without any extra argument passing, for myFunc . Answers: � Nodejs: get filename of caller function (4) Failing to restore the prepareStackTrace function can cause issues. Here's an example that removes side-effects. function

Not exactly answering the question here but some might appreciate this information.

With NodeJS & Forever(-monitor), the following contains a filename from which the process was started:

process.mainModule.filename

Haven't tried many uses™ though.

This seems to be a pretty decent explanation: https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi.

How can one get the file path of the caller function in node.js?, Contribute to sindresorhus/caller-path development by creating an account on GitHub. Get the path of the caller function. Important: npm install caller-path� within a module you can do any of the following to get the full path with filename. this.filename; module.filename; __filename; If you just want the actual name with no path or extension you can do something like this. module.filename.slice(__filename.lastIndexOf(path.sep)+1, module.filename.length -3);

The npm caller package has a function that returns the path and filename of the callers.

sindresorhus/caller-path: Get the path of the caller module, This is an example how to use stacktrace to find caller file in node function _getCallerFile() { try { var err = new Error(); var callerfile; var currentfile; Error. This is ticked, but doesn't answer the question, which is how to get the line number of the caller function. – mikemaccana Apr 17 '12 at 8:45 2 Also, this is extremely limited.

Use https://github.com/sindresorhus/callsites

If you use the first answer you could mess up with other libraries trying to do the same thing. See for example: https://github.com/facebook/jest/issues/5303

Node.js Recipes, Functions to get global instances Asynchronous thread-safe function calls lchmodSync(path, mode); DEP0037: fs.lchown(path, uid, gid, callback); DEP0038 : fs. Promise contexts may not get precise executionAsyncIds by default. @PatrickRoberts How do you want to use that in order to print the filename and the line of code in NodeJS? – lilezek Nov 3 '17 at 22:54 @lilezek if I was trying to answer the question as asked, I would have written an answer.

Node.js v14.7.0 Documentation, Example. Extract the filename from a file path: var path = require('path'); var filename = path.basename('/Users/Refsnes/demo_path.js'); console.log(filename );. Home » Nodejs » How to get a method name inside a method node.js How to get a method name inside a method node.js Posted by: admin December 16, 2017 Leave a comment

Node.js path.basename() Method, This is an example how to use stacktrace to find caller file in node function _getCallerFile() { try { var err = new Error(); var callerfile; var currentfile; Error. To offer protection against timing attacks and fingerprinting, the precision of Date.now() might get rounded depending on browser settings. In Firefox, the privacy.reduceTimerPrecision preference is enabled by default and defaults to 20µs in Firefox 59; in 60 it will be 2ms.

Nodejs: get filename of caller function, Get code examples like "Javascript get caller function" instantly right from get value from path variable node js � get value from serialized json� There does not seem to be a way to log the caller's method name and line number. I know that this has already been requested with #200, but that issue was locked with the reason that such a function would be very slow.

Comments
  • See also stackoverflow.com/questions/13227489/…
  • And see also stackoverflow.com/questions/10111163/…
  • See my answer here.
  • Great! I was trying to solve an issue related to that for hours. Reassigning the original function fixed everything.
  • hi, this solution works fine... but if a function was called in a callback then it will get the name of the file that fired the callback
  • Definitely doesn't answer the question but this helped me. Thanks!
  • The caller package returns internal/module.js when debugging.
  • The posted example on github also returns the same "internal/module.js" as caller on node v8.11.1
  • try { throw new Error(); } catch(e) { console.log(JSON.stringify(e)); } does not work on nodejs
  • You can't JSON encode native objects. Why would you even want to do that? :S
  • the caller is not always pre-determined, so: this way will not work
  • Resolve is used for merging paths, or traversing paths. To find the patch of a caller is different.