how to console.log the line number of the called function or console.log()
for example in the IDE i can see the line like this. how can I show that by console.log it
14
15 // other code/logic
16
17 console.error(`error at line ${herePutTheLogic}`);
18
output: error at line 17
>Solution :
You can use (new Error()).stack to get the stack trace at the current line. it would require some additional parsing to get the line number however.
e.g., console.log((new Error()).stack.split(':')[1]) will get it assuming it is at the root of the file, but once you start adding it to functions you’d need something a little more sophisticated.
console.log(
"❌ the error is on the line N." +
new Error().stack.split(":")[3]
);
