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

所以在发展中

npm start

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

pm2 start 'npm start'

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

forever start -c "npm start" ./

当前回答

开始前别忘了留出空间

pm2 start npm --[space]start

所以正确的命令是:

pm2 start npm -- start

其他回答

我需要在我的应用程序pm2中运行一个特定的npm脚本(对于每个env) 在我的例子中,是当我创建一个登台/测试服务时

对我有用的命令(参数必须这样转发):

pm2 start npm --name "my-app-name" -- run "npm:script"

例子:

pm2 start npm --name "myApp" -- run "start:test"

pm2 start npm --name "myApp" -- run "start:staging"

pm2 start npm --name "myApp" -- run "start:production"

希望有帮助

对于普通用户

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

您可以将目录更改为项目

cd /my-project

然后运行

pm2 start "npm run start" \\ run npm script from your package.json

点击这里阅读更多

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

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

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

pm2 start start.sh --name appNameYouLike

那些使用类似.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"
        }
    ]
}

这里有更多细节。