Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get more verbose logging in a Node application?

I have a Node application which defines some modules then starts an Express server. The problem is, sometimes the Express server doesn't start and I don't see any errors.

When this happens, I scan my git diff for potential syntax errors and fix them. This resolves the problem, and my server starts again.

The thing is, I'm not sure why these Syntax errors are failing silently.

I'll give an example:

  module.exports = function(server){
    this.register = function (params) {
      return this.Models.User.register(user)
      } // SYNTAX ERROR - extra closing bracket
    }
    return this
  }

When I removed the extra bracket, the server started again, but I would have thought I'd see a Syntax error raised instead of having to blindly hunt for bugs.

Some more context: My main.js file defines a function which returns a promise. The promise is invoked by running the script directly (nodejs main.js).

A lot of the code in this app is written with Promises. Would this have the effect of squelching errors? How can I ensure that errors are logged regardless of the scope they occur in?

like image 324
max pleaner Avatar asked Oct 29 '25 09:10

max pleaner


1 Answers

You can start logging unhandled promise rejections with:

process.on("unhandledRejection", (err) => {
   console.error(err); 
});

We added this hook about a year ago. I have plans to make logging or throwing the default behavior, I apologize for taking so long.

like image 55
Benjamin Gruenbaum Avatar answered Oct 30 '25 23:10

Benjamin Gruenbaum