[英]Using pm2 to do yarn start gives error while npm start works fine
我有一個 NodeJs 微服務。 做紗線啟動通常工作得很好。 當我嘗試使用 pm2 將其作為后台服務啟動時,面臨以下問題:
/Users/sairamk/.pm2/logs/api-error-21.log last 15 lines:
21|api | /usr/local/Cellar/yarn/0.27.5_1/bin/yarn:2
21|api | PREFIX="/usr/local" exec "/usr/local/Cellar/yarn/0.27.5_1/libexec/bin/yarn.js" "$@"
21|api | ^^^^
21|api |
21|api | SyntaxError: Unexpected identifier
21|api | at createScript (vm.js:74:10)
21|api | at Object.runInThisContext (vm.js:116:10)
21|api | at Module._compile (module.js:533:28)
21|api | at Object.Module._extensions..js (module.js:580:10)
21|api | at Module.load (module.js:503:32)
21|api | at tryModuleLoad (module.js:466:12)
21|api | at Function.Module._load (module.js:458:3)
21|api | at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:70:21)
21|api | at Module._compile (module.js:569:30)
21|api | at Object.Module._extensions..js (module.js:580:10)
我使用的 PM2 命令:
pm2 start yarn --name api -- start
雖然 npm start 相同,但使用以下命令可以正常工作:
pm2 start npm --name api -- start
嘗試探索許多可能性。 我做錯了什么?
您收到的錯誤是因為正在使用節點執行 bash 腳本( yarn
)...
因為 pm2 的默認解釋器設置為node
。
要運行 yarn 你必須將解釋器設置為bash
:
外殼:
pm2 start yarn --interpreter bash --name api -- start
或者當您使用ecosystem.config.js
:
module.exports = {
apps : [{
name : 'yarn',
script : 'yarn',
args : 'start',
interpreter: '/bin/bash',
env: {
NODE_ENV: 'development'
}
}]
};
您的 yarn 命令指向 bash 腳本/usr/local/Cellar/yarn/0.27.5_1/bin/yarn
而不是同一文件夾中的yarn.js
通過手動運行類似
pm2 start /usr/local/Cellar/yarn/0.27.5_1/bin/yarn.js --name api -- start
它應該按預期工作。 只需將路徑調整到當前位置即可。 就我而言,它是/usr/share/yarn/bin/yarn.js
。
作為旁注:像這樣執行 yarn 占用的內存幾乎是 npm 的兩倍。 我不知道確切的原因,但我會堅持使用 npm for pm2,因為理論上它應該沒有任何區別......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.