node.js pm2 可以运行“npm start”脚本吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31579509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:00:34  来源:igfitidea点击:

Can pm2 run an 'npm start' script

node.jsnpmpm2

提问by svnm

Is there a way for pm2 to run an npm start script or do you just have to run pm2 start app.js

有没有办法让 pm2 运行 npm start 脚本,或者你只需​​要运行 pm2 start app.js

So in development

所以在开发中

npm start

npm start

Then in production with pm2 you would run something like

然后在生产中使用 pm2 你会运行类似的东西

pm2 start 'npm start'

pm2 start 'npm start'

There is an equivalent way to do this in forever

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

forever start -c "npm start" ./

forever start -c "npm start" ./

回答by Dhaval Chauhan

PM2 now supports npm start:

PM2 现在支持 npm start:

pm2 start npm -- start

回答by Jyotman Singh

Those who are using a configuration script like a .jsonfile to run the pm2 process can use npm startor any other script like this -

那些使用像.json文件这样的配置脚本来运行 pm2 进程的人可以使用npm start或任何其他这样的脚本 -

my-app-pm2.json

我的应用程序pm2.json

{
    "apps": [
        {
            "name": "my-app",
            "script": "npm",
            "args" : "start"
        }
    ]
}

Then simply -

那么简单——

pm2 start my-app-pm2.json

Edit- To handle the use case when you have this configuration script in a parent directory and want to launch an app in the sub-directory then use the cwdattribute.

编辑- 当您在父目录中有此配置脚本并希望在子目录中启动应用程序时,要处理用例,然后使用该cwd属性。

Assuming our app is in the sub-directory nested-apprelative to this configuration file then -

假设我们的应用程序位于与nested-app此配置文件相关的子目录中,那么 -

{
    "apps": [
        {
            "name": "my-nested-app",
            "cwd": "./nested-app",
            "script": "npm",
            "args": "start"
        }
    ]
}

More detail here.

更多细节在这里

回答by jcollum

Yes. Use pm2 start npm --no-automation --name {app name} -- run {script name}. It works. The --no-automationflag is there because without it PM2 will not restart your app when it crashes.

是的。使用pm2 start npm --no-automation --name {app name} -- run {script name}. 有用。该--no-automation标志在那里,因为没有它 PM2 不会在崩溃时重新启动您的应用程序。

回答by peccu

I wrote shell script below (named start.sh). Because my package.jsonhas prestartoption. So I want to run npm start.

我在下面编写了 shell 脚本(名为start.sh)。因为我package.jsonprestart选择。所以我想跑npm start

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

Then, start start.shby pm2.

然后,从start.shpm2开始。

pm2 start start.sh --name appNameYouLike

回答by carkod

To use npm run

使用 npm run

pm2 start npm --name "{app_name}" -- run {script_name}

pm2 start npm --name "{app_name}" -- run {script_name}

回答by KARTHIKEYAN.A

Yes we can, now pm2 support npm start, --name to species app name.

是的,我们可以,现在 pm2 支持 npm start,--name 到物种应用程序名称。

pm2 start npm --name "app" -- start

回答by jdnichollsc

See to enable clustering:

请参阅启用集群:

pm2 start npm --name "AppName" -i 0 -- run start

What do you think?

你怎么认为?

回答by Kevin Cooper

If you use PM2 via node modules instead of globally, you'll need to set interpreter: 'none'in order for the above solutions to work. Related docs here.

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

In ecosystem.config.js:

ecosystem.config.js

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

回答by Tomer Omri

I needed to run a specific npm script on my app in pm2 (for each env) In my case, it was when I created a staging/test service

我需要在 pm2 中的应用程序上运行特定的 npm 脚本(对于每个 env)在我的情况下,它是在我创建临时/测试服务时

The command that worked for me (the args must be forwarded that way):

对我有用的命令(必须以这种方式转发 args):

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

examples:

例子:

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

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

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

Hope it helped

希望有帮助

回答by Luke Robertson

pm2 start npm --name "custom_pm2_name" -- run prod

pm2 start npm --name "custom_pm2_name" -- run prod

"scripts": {
    "prod": "nodemon --exec babel-node ./src/index.js"
  }

This worked for me when the others didnt

当其他人没有时,这对我有用