2017-09-16 417 views
0

我似乎在部署的實例中遇到了node_modules運行nodemon的問題。什麼是在部署環境中啓動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運行啓動我得到以下幾點:

[email protected]:/app# npm run start 
npm info it worked if it ends with ok 
npm info using [email protected] 
npm info using [email protected] 
npm info lifecycle [email protected]~prestart: [email protected] 
npm info lifecycle [email protected]~start: [email protected] 

> [email protected] start /app 
> nodemon 

sh: 1: nodemon: Permission denied 

npm info lifecycle [email protected]~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! [email protected] start: `nodemon` 
npm ERR! Exit status 126 
npm ERR! 
npm ERR! Failed at the [email protected] 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?

回答

0

這實際上是一個比問題更多的Linux問題,因爲它是一個權限問題 - 由npm運行的腳本nodemon沒有正確的權限。

如果使用npm run start調用nodemon有正確的權限(如根),npm將「交班」的執行,以nodemon並在這個過程可能是用戶切換到一個沒有root權限是安全的:

From the docs:

如果NPM用根特權被調用,則它會改變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

相關問題