NodeJS / ExpressJS: Syntax error unexpected end of file

2.3k Views Asked by At

One of my NodeJS / ExpressJS app gets quite from time to time by the error:

SyntaxError: Unexpected end of input
    at parse (native)

Unfortunately there is no line number and no stack. The hole project is based on typescript and compiles just fine. The process is running on a PM2.

Any idea how to hunt down this error?

Regards and thx...

1

There are 1 best solutions below

1
Yerken On

Search your app source code for JSON.parse, then find if any of the input your are getting is a proper JSON. I suspect you are getting HTML page as a response for one of your requests (due to 404 or 5xx) To track down the error:

try{
   JSON.parse(body);
}
catch(err){
  console.log('error due to ', err);
}