'Getting errors when trying to start app via yarn & pm2

Receiving an error when attempting to start node (next.js) app via yarn and pm2. My google fu didn't turn up anything useful. I suspect the issue is with nvm, though I don't have enough experience to understand how to fix it.

Ubuntu 20.04
nvm 0.38.0
node v14.5.0
yarn 1.22.10
pm2 4.5.6

ecosystem.config.js

module.exports = {
  apps: [
    {
        name: "next",
        script: "yarn",
        interpreter: "bash",
        args: "start:next",
        instances: 1,
        env: {
            NODE_ENV: "development",
        },
        env_staging: {
            NODE_ENV: "production",
        },
        env_production: {
            NODE_ENV: "production",
        }
    }
]

}

Starting file via:

pm2 start ecosystem.config.js --env staging

or

pm2 start yarn --interpreter bash --name next -- start:next

Receiving errors:

[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/home/deploy/.pm2/pm2.log last 15 lines:
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2        | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2        | 2021-04-08T18:33:28: PM2 log: Script /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn had too many unstable restarts (16). Stopped. "errored"

/home/deploy/.pm2/logs/express-out.log last 15 lines:
/home/deploy/.pm2/logs/express-error.log last 15 lines:
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 7: var: command not found
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: syntax error near unexpected token `('
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: `var majorVer = parseInt(ver.split('.')[0], 10);'
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 7: var: command not found
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: syntax error near unexpected token `('
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: `var majorVer = parseInt(ver.split('.')[0], 10);'
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express  | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found


Solution 1:[1]

--interpreter bash is your problem. yarn is not a bash script, rather what appears to be a JS program. Do this:

pm2 start yarn --name appname -- start:next

Solution 2:[2]

I found the only solution was to run it by defining a specific port, in my case 3000 thanks to the comment of Brandon Aguilar. That's the command

pm2 start yarn --name appname -- start -p 3000

It seems that by default it runs it to port 80

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0xLogN
Solution 2 Aspiiire