24

I have a website using nodejs. Problem is when user upload images the site stop working. That because of PM2 restart server when file change I think. How to solve this problem. thank you

Abhishek
  • 3,304
  • 4
  • 30
  • 44
Vuong Tran
  • 466
  • 1
  • 4
  • 12
  • pm2 does not automatically restart the processes it manages unless you specifically ask it to. Alternatively you could save the files in a folder outside of your working directory, which is actually better as the data should be separated from the code. Also please show us the log files by issuing `pm2 logs ` – Vasil Dininski Jun 28 '16 at 08:29

3 Answers3

44

PM2 has special flag --ignore-watch flag.

Try creating file process.json in the same directory where your app.js/index.js is and paste this:

{
  "watch": ["server", "client"],
  "ignore_watch" : ["node_modules", "public/images"],
  "watch_options": {
    "followSymlinks": false
  }
}

More on that topic: http://pm2.keymetrics.io/docs/usage/watch-and-restart/

Rafal Wiliński
  • 2,240
  • 1
  • 21
  • 26
19

A simple explanation, from actual experience

create a json file in the root folder of the the expressjs application. It can have any name, but I used pm2-process.json for clarity

{
    "script": "bin/www",
    "watch": true,
    "ignore_watch": ["log"],
    "watch_options": {
        "followSymlinks": false
    },
    "name": "YOUR_PM2_PROCESS_NAME"
}

To start your pm2 service from terminal, in the root folder of the express application:

pm2 start pm2-process.json

That's it. Really simple. There are many other options but this is the bare functional minimum .

Fields explanation:

  • script - the script to run the express application
  • watch - a boolean flag to control if pm2 watches (or not) the folder
  • ignore_watch - if watch is on, then tell pm2 which folders to ignore watching (in other words, this is a watch monitor exclusion list)
  • name - the name of the pm2 process ('service'). Set it to your application name of choice.

The full documentation is here: http://pm2.keymetrics.io/docs/usage/application-declaration/#attributes-available

Note: I left the node_modules folder out of the ignore_watch array in the example above, because I want pm2 to restart the service after a git pull and npm i that causes a change in the node modules. However it easy to ignore node_modules or any other folder (e.g., temp, public, etc.) by editing the array values

BigMan73
  • 1,344
  • 15
  • 14
0

Please use --spa

for example

pm2 serve build 3001 --name "any-name" --spa

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '23 at 07:25