'Nodemon and PM2 on VPS

I have a nodeJS app and I developp it with nodemon to restart everytime something change. So my package.json is like :

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
  },

But on production I have PM2. So do I need to keep nodemon with PM2 ? Because in my mind they do the same thing, I can --watch file with PM2 to restart automatically if anything change.

If yes, do I need to make a script for production (no nodemon) and another for dev?



Solution 1:[1]

You shouldn't need nodemon if you are using PM2, as you had stated, they are doing the same thing.

Nodemon is really more a development tool in my opinion, not to say you can't use it like this but most prod stacks will probably want to have clustering so pm2 would work better.

So..

local environment - Nodemon Prod - PM2 (clustering etc)

So in your ecosystems file, just set the watch flag then pm2 ecosystem file and you should be good to go..

Solution 2:[2]

I have a similar use case. I have a node.js application, app.js. I want node.js to automatically restart the application anytime the file app.js changes.

I had previously started my application with pm2 start app.js.

To accomplish the above, I simply entered the command pm2 retart app.js --watch.

The --watch parameter is documented here.

My environment: Ubuntu 20.04, node.js 14.19.1, pm2 5.2.0.

I did not install nodemon. Pm2 alone did the trick.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 proxim0
Solution 2 Steve Saporta