Search code examples
pm2serve

PM2 start script with multiple arguments (serve)


I'm trying to run serve frontend/dist -l 4000 from PM2. This is supposed to serve a Vue app on port 4000.

In my ecosystem.config.js, I have:

    {
      name: 'parker-frontend',
      max_restarts: 5,
      script: 'serve',
      args: 'frontend/dist -l 4000',
      instances: 1,
    },

But when I do pm2 start, in the logs I have the following message:

Exposing /var/lib/jenkins/workspace/parker/frontend/dist directory on port NaN

Whereas if I run the same command: serve frontend/dist -l 4000, it runs just fine on port 4000.


Solution

  • After running serve frontend/dist -l 5000 I got an error in the PM2 logs.

    In it's call stack I've found:

    at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/API/Serve.js:242:4)
    

    Notice the path: /usr/lib/node_modules/pm2/lib/API/Serve.js

    There is another command that's called serve in pm2 itself that was ran instead of the correct one. This is not the npm i -g serve I installed before. This is due to how Node package resolution works - it prioritizes local modules first.

    To use the globally installed version (the correct one), you need to specify the exact path to your global serve.

    To find out the path - on Linux, you can just do:

    $ which serve
    /usr/local/bin/serve
    

    Then put the path in your ecosystem.config.js script property.

    Final working ecosystem.config.js:

        {
          name: 'parker-frontend',
          script: '/usr/local/bin/serve', //pm2 has it's own 'serve' which doesn't work, make sure to use global
          args: 'frontend/dist -l 5000',
          instances: 1,
        },
        ```