Q:

how to get the line where the error : Cannot set headers after they are sent to the client is thrown?

As I'm fairly new to Node, i'm unable to know how to debug my code. In coding languages like c++ or python, our compiler tells us the line on which the error is
for ex: 

    print("Hello World")
    a===a

This will result in an error like:
'File "main.py", line 2                                                                                                       
    a===a                                                                                                                      
       ^                                                                                                                       
SyntaxError: invalid syntax'

but in node I get number of lines as error, like for this case:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:455:11)
at ServerResponse.header (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\express\lib\response.js:170:12)
at done (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\express\lib\response.js:1008:10)
at tryHandleCache (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\ejs\lib\ejs.js:260:5)
at View.exports.renderFile [as engine] (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\ejs\lib\ejs.js:485:10)
at View.render (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\express\lib\view.js:135:8)
at tryRender (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\express\lib\application.js:640:10)
at Function.render (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (D:\Node-Tries\22-07-19 to 26-07-19\node_modules\express\lib\response.js:1012:7)

I read a few posts about this error and from what I can tell, it happens if I try to send setheader info after already sending header body information? I am mainly using res.render and res.end in my program, so is there a way to debug the exact line/lines where the extra calls are being made?

A:

put this code on top of your code
Object.defineProperty(global, '__line', {
    get: function(){
        return ((new Error()).stack.split("\n")[2].trim().replace(/^(at\s?)(.*)/gim, "$2 >").replace(__dirname, ""))
    }
})

And then use console.log like:
console.log(__line, "somthing");

it will show you the line number and log

