NodeJS write log file (Json)

I want to write logs into a Json file

 newEntry =  "User: "  + lastUsername + " Time: "+now+ " Door: "+IOSDoor;
 lastUserOpenClose += newEntry;

 jsonString = JSON.stringify(lastUserOpenClose);

 fs.writeFile("lastUserOpenClose.json", lastUserOpenClose, function(err) {
            if(err)
            {
                console.log(err);
            }
            else
            {
                console.log("Server-Log: The file was saved!");
            }
        });

But im overwriting the logs. How to write somthing into a json file and don't overwrite the old logs?

You should use the fs.appendFile

Example:

fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
  console.log('The "data to append" was appended to file!');
});