Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 236
Sairam Krish Avatar asked Aug 25 '17 18:08

Sairam Krish


People also ask

What is the difference between npm start and yarn start?

npm: npm fetches dependencies from the npm registry during every 'npm install' command. Yarn: yarn stores dependencies locally, and fetches from the disk during a 'yarn add' command (assuming the dependency(with the specific version) is present locally).

Why npm start is not working?

Check the ignore-script config If you see the start script is present inside your package. json file but still can't run the script, you need to check the console output. If there's no output at all, then you may have the ignore-scripts npm configuration set to true .


2 Answers

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

Because pm2's default interpreter is set to node.

To run yarn you'll have to set the 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'
    }
  }]
};
like image 157
chimurai Avatar answered Oct 28 '22 00:10

chimurai


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...

like image 23
Christian Ivicevic Avatar answered Oct 28 '22 00:10

Christian Ivicevic