titusmagnus
titusmagnus

Reputation: 2082

Obtaining the name of the current function being executed in Node.js

In Node.js, I'm trying to obtain the name of current function being executed:

function doSomething(req, res) {
  console.log('Function name: ' + doSomething.name);
}

This works well, but I'd like (if possible) to obtain the name within the current context. This way, if I renamed the method later I don't have change it manually. Is there a generic object (i.e. similar to 'this') that points to the current function being executed/current context? Thank you.

Upvotes: 22

Views: 14012

Answers (4)

AWE FRANCIS OLAWUMI
AWE FRANCIS OLAWUMI

Reputation: 225

This is a use case for finding error specific information about a function or method. The function name is gotten from error stack info. The printError method will return the function name, where the function is used, where the error occured and where the function is implemented with the error message.

// You can try this out

const extractFunctionName = (stackLine) => {
  const regex = /at (.*?) \(/;
  const match = stackLine.match(regex);
  return match ? match[1] : 'unknown';
}

const printError = (e) => {
   const regex = /\((.*):(\d+):(\d+)\)$/
   const caller = regex.exec(e?.stack.split("\n")[2]);
   const issue = regex.exec(e?.stack.split("\n")[1]);

   let formattedErr = {
      functionName: extractFunctionName(issue["input"]),
      calledIn: caller[1],
      calledAt: `${caller[2]}:${caller[3]}`,
      implementIn: issue[1],
      errorAt: `${issue[2]}:${issue[3]}`,
      message: e?.message,
    };

  console.log("Error trace ", formattedErr);
  return formattedErr;
}


function doSomething () {
   try {
    let a = 18; let c;
    console.log("calculation started...");
    c = b/a;
    console.log("result ", c)
 }
  catch (err) {
    printError(err)
  }
 }

 // invoke function
 doSomething()

Upvotes: 0

marko424
marko424

Reputation: 5356

You can use property name as:

const yourFunction = () => {};

console.log(yourFunction.name);
// output: "yourFunction"

MDN reference

Upvotes: 1

Cleber Machado
Cleber Machado

Reputation: 316

Short answer: Object.values(this)[0].name

Long Answer:

let myInstanceArray = Object.values(this) Enumerates the object properties as an array

let myInstance = myInstanceArray[0] Gets the first, and only, item in the array

let myName = myInstance.name Gets the name of the item.

Upvotes: 6

basilikum
basilikum

Reputation: 10526

I don't want to repeat the answers in the "possible duplicate" suggestions from Ian, but there is a solution that might be worth mentioning in addition to them:

You could use a named function expression, to have one name, that is accessible from outside of the function and one name that is accessible from inside:

var x = function y() {
    console.log(y);
};

console.log(x);

Now, if you decide to give your function a different name, you can just change x while still be able to refer to the function by using y.

Upvotes: 1

Related Questions