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.
Here is a one liner for quick debugging purposes:
console.log("DEBUG", (new Error().stack.split("at ")[1]).trim());
This will log something like this with Node.js:
DEBUG SomeObject.function (/path/to/the/code.js:152:37)
--
You can also add custom args at the end, e.g.
console.log("DEBUG", (new Error().stack.split("at ")[1]).trim(), ">>>", myVar);
Note that if you put this into a helper function, adjust the stack index from e.g. [1]
to [2]
.