Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to console.log the number of the line in the code

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>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]
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading