Search code examples
vagrantpm2

How to get PM2 to watch for changes in Vagrant's shared directory?


I am using Vagrant 1.8.1 and VirtualBox 5.0.16r105871 and provisioning a ubuntu/wily64 box. I installed Node 4.4.3 on the guest. I use the following process to get the server up and running.

cd /vagrant
npm install
npm install -g pm2
pm2 startup ubuntu
sudo su -c "env PATH=$PATH:/usr/bin pm2 startup ubuntu -u vagrant"
pm2 start process.json
pm2 save

I tried starting PM2 using the --watch option be that didn't work. I tried using PM2 process configuration but it only works if I touch files while on the guest machine.

{
  "apps": [{
    "name": "mean-boilerplate",
    "script": "index.js",
    "watch": ["index.js"],
    "env": {
      "NODE_ENV": "development",
    }
  }]
}

Any ideas how to get this working with PM2? Or should I be using a different method/module.


Solution

  • The usePolling option made the watch functionality work for me when I needed to watch folders that reside on the host but are mounted in the Vagrant VM (like the shared directory). Try adding it like this to your JSON config:

    {
      "apps": [{
        "name": "mean-boilerplate",
        "script": "index.js",
        "watch" : ["index.js"],
        "watch_options" : {"usePolling": true},
        "env": {
          "NODE_ENV": "development",
        }
      }]
    }