是否有一种方法让pm2运行npm启动脚本,或者你只需要运行pm2 start app.js
所以在发展中
npm start
然后在使用pm2的生产中,你会运行这样的程序
pm2 start 'npm start'
有一种等效的方法可以永远做到这一点:
forever start -c "npm start" ./
是否有一种方法让pm2运行npm启动脚本,或者你只需要运行pm2 start app.js
所以在发展中
npm start
然后在使用pm2的生产中,你会运行这样的程序
pm2 start 'npm start'
有一种等效的方法可以永远做到这一点:
forever start -c "npm start" ./
当前回答
它在CentOS 7上运行良好
PM2版本4.2.1
让我们举两个例子:
1. npm start //server.js
pm2 start "npm -- start" --name myMainFile
2. NPM运行main //main.js
pm2 start "npm -- run main" --name myMainFile
其他回答
是的。使用pm2启动npm——no-automation——name{应用程序名称}——运行{脚本名称}。它的工作原理。——no-automation标志的存在是因为如果没有它,PM2将不会在应用崩溃时重新启动应用。
那些使用类似.json文件的配置脚本来运行pm2进程的人可以使用npm start或任何其他类似的脚本
my-app-pm2.json
{
"apps": [
{
"name": "my-app",
"script": "npm",
"args" : "start"
}
]
}
那么简单——
pm2 start my-app-pm2.json
Edit -当你在父目录中有这个配置脚本,并且想要在子目录中启动应用程序时,使用cwd属性来处理用例。
假设我们的应用程序在相对于这个配置文件的nested-app子目录中,那么-
{
"apps": [
{
"name": "my-nested-app",
"cwd": "./nested-app",
"script": "npm",
"args": "start"
}
]
}
这里有更多细节。
首先,您需要创建一个run.js文件,并将下面的代码粘贴到该文件上。
const { spawn } = require('child_process');
//here npm.cmd for windows.for others only use npm
const workerProcess = spawn('npm.cmd', ['start']);
workerProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
workerProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
workerProcess.on('close', function (code) {
console.log('child process exited with code ' + code);
});
然后用pm2运行这个文件。
pm2 start run.js
列表项
如果你通过节点模块而不是全局使用PM2,你需要设置interpreter: 'none'以使上述解决方案工作。相关文档在这里。
在ecosystem.config.js:
apps: [
{
name: 'myApp',
script: 'yarn',
args: 'start',
interpreter: 'none',
},
],
不幸的是,pm2似乎不支持您要求的确切功能https://github.com/Unitech/PM2/issues/1317。
建议的替代方案是使用生态系统。开始部署,其中可能包括生产和开发环境的设置。然而,这仍然使用npm start来引导你的应用程序。