Sam Sverko
Sam Sverko

Reputation: 1600

Why does pm2 watch restart over and over?

I've been working with pm2 for a few weeks, using the --watch flag with no issues.

Then today, when launching even the most basic Node.js + Express.js app, it constantly reloads over and over, without end.

Command: pm2 start server.js --watch (results in pm2 id of 0).

server.js

const express = require('express');
const app = express();

app.listen(3000, () => {
    console.log('Server successfully started and listening at http://localhost:3000.');
});

I even using pm2 stop 0 --watch


On some occasions, I've noticed that even though the pm2 instance is stopped (and even deleted), the port remains open. So I've been using the terminal command sudo lsof -iTCP -sTCP:LISTEN -n -P to list open ports, along with sudo kill -9 PID to kill the open port. No idea why that happens. Not sure if that is relevant.

Upvotes: 1

Views: 3619

Answers (2)

Farhan Ali
Farhan Ali

Reputation: 51

I was going crazy because I had tried everything to stop pm2 from watching a directory. At the end I just deleted the processes and started all over again. I think my mistake was that the first time I had watch: "." set as property in ecosystem.config.js. And I am guessing no matter what I did, that setting couldn't be changed. So as of now, this is my ecosystem.config.js:

module.exports = {
  apps : [{
    name: 'atelier',
    script: 'server/index.js',
    watch: false,
    ignore_watch: ["uploads/tempImage" ],
    instances: 'max',
    env_production: {
      NODE_ENV: 'production'
    }
  }]
};

Upvotes: 0

mohammad javad ahmadi
mohammad javad ahmadi

Reputation: 2081

I think you should make a processes.json and run it. update that with this option for running and watching:

//processes.json:
{
  "apps" : [{
    "name": "express-app",
    "script": "server.js",
    "watch": ["server.js"],
    "ignore_watch": ["node_modules"],
    "watch_options": {
      "usePolling": true,
      "alwaysStat": true,
      "useFsEvents": false
    },
    "env": {
        "NODE_ENV": "development"
    }
  }]
}

Put that on the root of your project, then run your pm2 as so:

pm2 start processes.json

Upvotes: 1

Related Questions