【问题标题】:Whats the proper way to start nodemon on a deploy environment?在部署环境中启动 nodemon 的正确方法是什么?
【发布时间】:2018-02-25 15:31:10
【问题描述】:

我似乎在已部署实例中从 node_modules 运行 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 run 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?

【问题讨论】:

    标签: node.js npm nodemon


    【解决方案1】:

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

    如果您使用npm run start 调用nodemon 并且 具有正确的权限(例如root),npm 将“移交”执行到nodemon 并在此过程中为了安全起见,可能将用户更改为没有 root 权限的用户:

    From the docs:

    如果 npm 是用 root 权限调用的,那么它会改变 uid 到用户配置指定的用户帐户或uid,其中 默认为无人。设置 unsafe-perm 标志以使用 root 运行脚本 特权。

    如果您自己运行node_modules/nodemon/bin/nodemon.js(并且您具有root 权限),它会绕过“移交”,以便nodemon.js 以root 权限运行。

    部署节点应用程序最正确的方法是使用pm2 之类的东西来管理进程,而不是使用nodemon,因为nodemon 主要用于监视更改和重新启动服务器(其中主要只在开发环境中有用)。如果还想用nodemon,可以combine it with the forever package with nodemon like explained here

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 2019-04-15
      • 2022-06-24
      • 2015-03-14
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多