Monday 29 February 2016

How do you identify the line number of an error in your Node.js code?

I am mostly loving the experience of coding in Node/JS. The hardest part, though, is identifying where an error is triggered - and was wondering if anyone here has feedback.I'll often make a mistake (not close a bracket, skip a comma in an object declaration, pass a bad variable to an external library call, leave a variable undefined), and I've found these really hard to debug for a few reasons:import statements seem to fail without complaining (when there is an error in the imported file), so that errors are thrown on the later call to a function in the imported file (which is now undefined) - using Express (where I have routes in app.js and then import various controllers), this often leads to a hanged or a function not found error, WITHOUT any indication of a line number of where the issue could beWhen there is a stack trace (esp with external libraries), it often doesn't reference my code at any point of the stack trace; I'm assuming this has to do with the async nature of js, but it makes it really hard to find where the issue is happening (example, a Sequelize ORM call fails with a cryptic message, a stack trace 15 deep referring to code in the library and promise library, but no line number for the originating call in my code)I have taken to naming every function (so no anonymous functions), and this doesn't seem to help significantly (and also I don't know how to make this work for ES6's new arrow functions).What is best practice to get more descriptive error messages?

Submitted February 29, 2016 at 06:28PM by Federalist85

No comments:

Post a Comment