Using pm2 to do yarn start gives error while npm start works fine
Asked Answered
L

2

13

I have a NodeJs microservice. Doing yarn start normally works perfectly fine. When I try to use pm2 to start this as a background service, facing the below issue:

/Users/sairamk/.pm2/logs/api-error-21.log last 15 lines:
21|api     | /usr/local/Cellar/yarn/0.27.5_1/bin/yarn:2
21|api     | PREFIX="/usr/local" exec "/usr/local/Cellar/yarn/0.27.5_1/libexec/bin/yarn.js" "$@"
21|api     |                     ^^^^
21|api     |
21|api     | SyntaxError: Unexpected identifier
21|api     |     at createScript (vm.js:74:10)
21|api     |     at Object.runInThisContext (vm.js:116:10)
21|api     |     at Module._compile (module.js:533:28)
21|api     |     at Object.Module._extensions..js (module.js:580:10)
21|api     |     at Module.load (module.js:503:32)
21|api     |     at tryModuleLoad (module.js:466:12)
21|api     |     at Function.Module._load (module.js:458:3)
21|api     |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:70:21)
21|api     |     at Module._compile (module.js:569:30)
21|api     |     at Object.Module._extensions..js (module.js:580:10)

PM2 command that I use:

pm2 start yarn --name api -- start

while npm start for the same, works fine with below command :

pm2 start npm --name api -- start

Tried exploring many possibilities. What am I doing wrong ?

Lying answered 25/8, 2017 at 18:7 Comment(1)
you should open up an issue here: github.com/yarnpkg/yarn/issuesDictator
M
45

The error you're getting is because a bash script (yarn) is being executed with node...

pm2's default interpreter is set to node.


To run yarn (bash script) correctly, you'll have to set pm2's interpreter to bash:

shell:

pm2 start yarn --interpreter bash --name api -- start

or when you are using the ecosystem.config.js:

module.exports = {
  apps : [{
    name      : 'yarn',
    script    : 'yarn',
    args      : 'start',
    interpreter: '/bin/bash',
    env: {
      NODE_ENV: 'development'
    }
  }]
};
Mashburn answered 17/8, 2018 at 20:57 Comment(5)
Thanks, from today I got an error that my interpret was not defined. Before I didn't need it. Your solution worked for me!Till
You saved me from 2 days of hell, thanks!Pronator
how can I configure it on a windows server? I've tried but failed cause of the interpreter (my suspection). If I run plain yarn serve: shop command in command prompt same folder level that ecosystem.config.js is existed working as expected.Ferreous
Every issue I've found says this is the fix, but it's not working for me - this is the error I'm getting: SyntaxError: missing ) after argument list at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1033:15) at Module._compile ...Clew
Also beware, the yarn --cwd option will not work here but you can add a cwd entry to your app configRunway
F
2

Your yarn command is pointing to a bash script /usr/local/Cellar/yarn/0.27.5_1/bin/yarn and not the yarn.js in the same folder. By manually running something like

pm2 start /usr/local/Cellar/yarn/0.27.5_1/bin/yarn.js --name api -- start

it should work as expected. Just adapt the path to the current location. In my case it was /usr/share/yarn/bin/yarn.js.


As a sidenote: executing yarn like this takes up almost double the memory than compared to npm. I don't know the exact reason, but I'll stick to npm for pm2 since it should not make any difference, in theory...

Flight answered 7/8, 2018 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.