在部署环境中启动 nodemon 的正确方法是什么?

Whats the proper way to start nodemon on a deploy environment?

我似乎在已部署实例中遇到来自 node_modules 的 运行ning nodemon 问题。

我的package.json

中大致有这个
{
   ...
  "version": "0.0.3",
  "main": "dist/src/server.js",
  "description": "Persistence Layer",
  "engines": {
    "node": "~6.7"
  },
  "scripts": {
    "start": "nodemon",
  },
"dependencies": {
    ...
    "nodemon": "^1.11.0",
    ...
  }
}

我的 nodemon.json 文件中有以下内容

{
  "restartable": "rs",
  "verbose": true,
  "debug": 5858,
  "delay": 1,
  "watch": [
    "dist/",
    "node_modules/"
  ],
  "ext": "js",
  "args": [
    "--debug=5858",
    "--max_old_space_size=6384",
    "--optimize_for_size",
    "--max_executable_size=6384",
    "--stack_size=6384"
  ]
}

当我尝试 npm 运行 start 时,我得到以下信息:

jrlil@28178a64e860:/app# npm run start
npm info it worked if it ends with ok
npm info using npm@3.10.8
npm info using node@v6.9.1
npm info lifecycle api@0.0.3~prestart: api@0.0.3
npm info lifecycle api@0.0.3~start: api@0.0.3

> api@0.0.3 start /app
> nodemon

sh: 1: nodemon: Permission denied

npm info lifecycle -api@0.0.3~start: Failed to exec start script
npm ERR! Linux 3.10.0-514.16.1.el7.x86_64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start"
npm ERR! node v6.9.1
npm ERR! npm  v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! -api@0.0.3 start: `nodemon`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the -api@0.0.3 start script 'nodemon'.
npm ERR! Make sure you have the latest version of node.js and npm installed.

但是,当我 运行 它使用以下内容时,一切都按预期工作。

$node node_modules/nodemon/bin/nodemon.js
[nodemon] 1.12.1...

为什么 npm run 无法查看我的 node_modules 文件夹并启动 nodemon?

这实际上更像是一个 Linux 问题而不是节点问题,因为它是一个权限问题 -- npm 运行 的脚本 nodemon 没有正确的权限。

如果您使用 npm run start 调用 nodemon 并且 拥有正确的权限(例如 root),npm 将 "hand off" 执行 nodemon 并在此过程中可能将用户更改为没有 root 权限的用户以确保安全:

From the docs:

If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.

如果你 运行 node_modules/nodemon/bin/nodemon.js 你自己(并且你有 root 权限),它会绕过 "hand off" 这样 nodemon.js 就是 运行 具有 root 权限.

部署节点应用程序的最正确方法是使用类似pm2 to manage processes, and not use nodemon since nodemon is mostly used to watch for changes and restart the server (which is mostly only useful in development contexts). If you still want to use nodemon, you can combine it with the forever package with nodemon like explained here的方法。