Getting errors when trying to start app via yarn & pm2
Asked Answered
P

2

5

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
Perk answered 8/4, 2021 at 19:23 Comment(3)
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.Egypt
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.Perk
Also, note that pm2 start yarn --interpreter bash --name appname -- start:next fails with the same errors above.Perk
A
16

--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
Arrangement answered 9/4, 2021 at 3:16 Comment(3)
Worked, thank you! Something must have changed recently. I've been doing it this way. #59047337Perk
I think Yarn v2 migrated to Node?Arrangement
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).Whom
V
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

Vivle answered 28/4, 2022 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.