max pleaner
max pleaner

Reputation: 26778

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?

Upvotes: 4

Views: 4987

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

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.

Upvotes: 2

Related Questions