Help us understand the problem. What is going on with this article?

[Node.js][PM2] Vagrant 共有フォルダのファイルを watch する方法

More than 1 year has passed since last update.

宮崎在住エンジニアのジョウ(@JotarO_Oyanagi) です。

Vagrant を Node.js の実行環境として使用していて、ホットリロードとして PM2(Node アプリをデーモン化してくれる)の watch を使う場合、普通の設定だとホットリロードが効かないので、その解決策メモです。

TL;DR

  • 環境:centos 7.5, node 8.11.4
  • pm2 startecosystem.config.js または config.json を使用して実行
  • watch_optionsusePolling: true を設定

書き方の例

ecosystem.config.js
module.exports = {
  apps : [{
    name: 'MyApp',
    script: 'app.js',
    watch: true,
    watch_options: {
      usePolling: true
    },
    env: {
      NODE_ENV: 'development',
      name: 'MyApp_dev',
      PORT: 3000,
      TZ: "Asia/Tokyo"
    }
  }]
};
config.json
{
  "name": "MyApp",
  "script": "./app.js",
  "watch": true,
  "watch_options": {
    "usePolling": true
  },
  "env": {
    "NODE_ENV": "development",
    "name": "MyApp_dev",
    "PORT": 3000,
    "TZ": "Asia/Tokyo"
  }
}

これだけです。カンタンですね!

終わりに

色々探し回った結果、これだけでよかったのかよ!という複雑な気持ち。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away