是否有一种方法让pm2运行npm启动脚本,或者你只需要运行pm2 start app.js

所以在发展中

npm start

然后在使用pm2的生产中,你会运行这样的程序

pm2 start 'npm start'

有一种等效的方法可以永远做到这一点:

forever start -c "npm 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 

列表项

其他回答

它在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

我在下面写了shell脚本(名为start.sh)。 因为我的包裹。Json有预启动选项。 我想运行npm start。

#!/bin/bash
cd /path/to/project
npm start

然后,通过pm2启动start.sh。

pm2 start start.sh --name appNameYouLike

使用npm运行

Pm2启动NPM——name "{app_name}"——run {script_name}

如果你通过节点模块而不是全局使用PM2,你需要设置interpreter: 'none'以使上述解决方案工作。相关文档在这里。

在ecosystem.config.js:

  apps: [
    {
      name: 'myApp',
      script: 'yarn',
      args: 'start',
      interpreter: 'none',
    },
  ],

对于普通用户

PM2现在支持npm start:

pm2 start npm -- start

要给PM2进程分配一个名称,使用"——name"选项:

pm2 start npm --name "your desired app name" -- start

对于root用户

sudo pm2 start npm -- start

要给PM2进程分配一个名称,使用"——name"选项:

sudo pm2 start npm --name "your desired app name" -- start