pm2 deploy nvm not loaded

// pm2 ecosystem.config.js
module.exports = {  
  deploy : {
    production : {
      user : "node",
      host : "xxx.xxx.xxx.xxx",
      ref  : "origin/master",
      repo : "https://github.com/fourcels/ghostiny.git",
      path : "~/ghostiny",
      "post-deploy" : "pm2 reload ghost"
    }
  }
}
$ pm2 deploy production
  ...
  ○ executing post-deploy `pm2 reload ghost`
  bash: pm2: command not found
  Deploy failed

原因分析

pm2 deploy 采用 ssh command 的方式执行命令, ~/.bashrc 判断不是交互模式, 就会直接退出

# If not running interactively, don't do anything
case $- in  
    *i*) ;;
      *) return;;
esac  

输出 $-, $PATH

~ ᐅ ssh node@xx.xx.xxx.xxx 'echo $PATH'
/usr/local/bin:/usr/bin:/bin:/usr/games # 没有 nvm 目录

~ ᐅ ssh node@xx.xx.xxx.xxx 'echo $-'
hBc # 没有 i option flag  

解决方法

nvm 配置放在判断是否交互模式之前

export NVM_DIR="/home/node/.nvm"  
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

# If not running interactively, don't do anything
case $- in  
    *i*) ;;
      *) return;;
esac  

输出 $PATH

~ ᐅ ssh node@xx.xx.xxx.xxx 'echo $PATH'
/home/node/.nvm/versions/node/v6.9.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/games