Search code examples
node.jsubuntupm2

How to use pm2 start npm in a specific directory?


Is there a CLI tag for running pm2 start npm -- start in a specific directory? I looked around but could not find an answer.

On the other hand, when I run pm2 without npm, I can specify which directory I want to run pm2 in. For example:

pm2 start /opt/www/myapp/index.js

Is there any way to add a path tag to the pm2 start npm -- start command?


Solution

  • You can use something like this:

    cd /directory/of/my/app ; pm2 start npm -- start
    

    You can also write an ecosystem file to parameter you app:

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

    To generate an empty ecosystem file:

    pm2 init simple
    

    this will generate a file named ecosystem.config.js that you can rename.

    Then to start application:

    pm2 start ecosystem.config.js
    

    ecosystem doc: https://pm2.keymetrics.io/docs/usage/application-declaration/