Mr.Smith
Mr.Smith

Reputation: 190

Nodejs - how to track after errors?

Yesterday I deployed my first nodejs app everything was fine, I woke up today and I saw the server stoped because error with some package. How can I track after all the errors, and make the server not to go down even there is some error?

Upvotes: 0

Views: 245

Answers (2)

Micha
Micha

Reputation: 924

You can use nodemon to restart the node-server after crashing - see here:

install:

npm install -g nodemon

run: nodemon -x 'node app.js || touch app.js'


alternative this should also work:

npm install pm2 -g
pm2 start server.js --watch

Upvotes: 1

l2ysho
l2ysho

Reputation: 3063

You can use PM2 as a process manager

npm install pm2 -g

Good practice is generate a ecosystem config

pm2 ecosystem

ecosystem.config.ts

    apps: [
        {
            name: 'My application',
            script: 'npm',
            args: 'start',
            autorestart: true,
            instance_var: 'my-app',
            out_file: '/path/to/out.log',
            log_file: '/path/to/global.log',
            error_file: '/path/to/error.log'
        }
    ]
};

Also you can use npx instead of globally install pm2. You can start you app with pm2 start ecosystem.config.js (or npx pm2 start ecosystem.config.js if you like).

To see list of started processes and its statuses use pm2 list or npx pm2 list

Upvotes: 2

Related Questions