Sign in

How to restart automatically after a node crash?

superrache edited in Sun, 19 Jun 2022
When I was playing on the chrome console, I made a mistake and the node process crashed
I have to restart the node manually. How can I restart the node automatically? I just want to quickly verify some code on the console
In the following code, I deliberately did not catch the error and requested a file that does not exist. I hope it can be restarted quickly


This is a basic server example, using debug mode to set up the environment
node --inspect aaa.js

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World!');
8 Replies
commented on Sun, 19 Jun 2022

The best toys

gtihub: https://github.com/GoogleChro...


  1. The error process does not crash
  2. Code automatically updated, similar to nodemon and PM2, but the official use of no restart technology
  3. Code bidirectional update, after the debugger is modified, the source file is also modified
  4. The terminal is attached
  5. One click refresh environment, just like browser refresh




commented on Sun, 19 Jun 2022

There is an automatic restart method in the building, so I won't say more. In fact, there is no need to restart, just do not exit. The specific method is to catch "uncapped exceptions":

process.on('uncaughtException', err => {
  console.error(err && err.stack)
commented on Sun, 19 Jun 2022

PM2, forever and other process managers

commented on Mon, 20 Jun 2022

Debug nodemon locally, modify code and restart automatically

commented on Mon, 20 Jun 2022

PM2 is recommended for management

https :// www . jianshu . com / p / f64 ...
commented on Mon, 20 Jun 2022

You can learn about forever, which is very easy to use. After loading, start your project with forever to solve the problem

commented on Mon, 20 Jun 2022

Nodemon is recommended in the development phase

Introduction to nodemon

PM2 is recommended in the production stage

PM2 introduction

PM2 or forever can be used in the test phase

Introduction to forever

commented on Tue, 21 Jun 2022

Linux and MAC development environment:

  1. Supervisor, nodemon and other daemons tools can be used for debugging
  2. PM2 process monitoring tool can be used to publish projects
lock This question has been locked and the reply function has been disabled.