Search code examples
node.jsnpmpm2nvm

pm2 error when launching apps using `ecosystem.config.js`


When I launch my apps independently there's no problem but to simplify hosting, I want pm2 to launch them using a ecosystem.config.js file.

Here's my ecosystem.config.js :

module.exports = {
  apps: [
    {
      name: "strapi_portfolio",
      cwd: "./server",
      script: "npm",
      args: "start",
      exec_mode: "fork",
      exec_interpreter: "bash",
      exec_args: "-c 'npm run build && npm start' ",
      autorestart: true,
    },
    {
      name: "nextjs_portfolio",
      cwd: "./client",
      script: "npm",
      args: "start",
      exec_mode: "fork",
      exec_interpreter: "bash",
      exec_args: "-c 'npm run build && npm start' ",
      autorestart: true,
      wait_ready: true,
      listen_timeout: 60000,
    },
  ],
};

But when I execute the pm2 start command, I get this error (It does it for both strapi_portfolio and nextjs_portfolio):

0|strapi_p | /root/.nvm/versions/node/v18.15.0/bin/npm: line 2: require('../lib/cli.js')(process)' 0|strapi_p | /root/.nvm/versions/node/v18.15.0/bin/npm: line 2: syntax error near unexpected token '../lib/cli.js'

I'm using nvm to manage my node versions on my server.

Do you have an idea of what could be causing this error ? Thanks in advance !


Solution

  • This was due to using a wrong node version.

    I solved this issue by specifying the right node interpreter

    module.exports = {
      apps: [
        {
          name: "strapi_portfolio",
          interpreter: "node@16.16.0", // HERE
          cwd: "./server",
          script: "npm",
          args: "run start-prod",
          autorestart: true,
        },
        {
          name: "nextjs_portfolio",
          interpreter: "node@16.16.0", // HERE
          cwd: "./client",
          script: "npm",
          args: "run start-prod",
          autorestart: true,
          wait_ready: true,
          listen_timeout: 60000,
        },
      ],
    };