4

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
David S
  • 325
  • 4
  • 15
  • could you share your project directory structure. Yarn is a development tool, used to manage dependencies and run whatever scripts you have defined in project.json. After you build an app for production, you would use pm2 to run the app. You are trying to run YARN itself and that isn't going to work. Once you share your directory ill provide an answer. – Dallas Baker Apr 08 '21 at 22:11
  • In my package.json scripts `start:next` runs `next start`. If I `yarn start:next` in project dir, it runs fine. The command to start an app via yarn in pm2, you'd use `pm2 start yarn --interpreter bash --name appname -- start`, replacing start with whatever script defined in package.json. Within the ecosystem file I'm essentially doing the same thing as far as I can tell. – David S Apr 09 '21 at 00:15
  • Also, note that `pm2 start yarn --interpreter bash --name appname -- start:next` fails with the same errors above. – David S Apr 09 '21 at 00:26

2 Answers2

14

--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
0xLogN
  • 3,289
  • 1
  • 14
  • 35
  • Worked, thank you! Something must have changed recently. I've been doing it this way. https://stackoverflow.com/questions/59046837/what-is-the-pm2-for-command-yarn-run-start – David S Apr 09 '21 at 18:21
  • I think Yarn v2 migrated to Node? – 0xLogN Apr 09 '21 at 22:03
  • 2
    Nice, for anyone that is having "problems" with this command with a port specification: I ran ```pm2 start yarn --name appname -- start -p 80``` and basically it runs a ```next start -p 80``` (for the case of nextjs). – Brandon Aguilar Nov 12 '21 at 00:32
0

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

Aspiiire
  • 308
  • 3
  • 7