Pm2: Issue with yarn when using pm2

Created on 8 May 2018  路  9Comments  路  Source: Unitech/pm2

I get this error when trying to use yarn with pm2:

4|aggregat | /usr/share/yarn/bin/yarn:2
4|aggregat | argv0=$(echo "$0" | sed -e 's,\\,/,g')
4|aggregat |         ^^^^
4|aggregat |
4|aggregat | SyntaxError: missing ) after argument list
4|aggregat |     at createScript (vm.js:80:10)
4|aggregat |     at Object.runInThisContext (vm.js:139:10)
4|aggregat |     at Module._compile (module.js:616:28)
4|aggregat |     at Object.Module._extensions..js (module.js:663:10)
4|aggregat |     at Module.load (module.js:565:32)
4|aggregat |     at tryModuleLoad (module.js:505:12)
4|aggregat |     at Function.Module._load (module.js:497:3)
4|aggregat |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:83:21)
4|aggregat |     at Module._compile (module.js:652:30)

The weird thing is it works fine when I cd into the directory and run yarn start, but when using pm2 it doesn't work and I get the above error. Any ideas?

stale

Most helpful comment

Got the same issue out of the blue, changing my conf to this worked for me:

{
      "name"        : "my_app",
      "script"      : "yarn run server",
      "args"        : "",
      "watch"       : true,
      "node_args"   : "",
      "merge_logs"  : true,
      "cwd"         : "/apps/www/api.com",
      "env": {
        "NODE_ENV": "production"
      }
    }

this is what I had initially

{
      "name"        : "my_app",
      "script"      : "yarn",
      "args"        : "run server",
      "watch"       : true,
      "node_args"   : "",
      "merge_logs"  : true,
      "cwd"         : "/apps/www/api.com",
      "env": {
        "NODE_ENV": "production"
      }
    }

All 9 comments

I somehow got things working after hours of trying.

I used npm to run the project alongside yarn install. npm install had its own issues. Still couldn't get things working with yarn start unfortunately.

Having the same error - can't run yarn with pm2. Without pm2 it works fine. Also npm works fine.

anyone fixed this yarn thing or is it yarn itself ?

Okay so i wanna add my experience to this in case it helps anyone.

A few months ago i installed node, yarn and pm2 on an experimental server. I managed to get pm2 to work with yarn with no problem.
Now, i am ready to deploy my app. So i moved it to a clean server and i installed all the packages properly without sudo. I ran into the exact issue mentioned here.

At first, i thought maybe it's a problem with the latest version. So i downgraded the node, yarn and pm2 versions on my second server to match the first (experimental) server. Still didn't work.
Then i thought there is only one thing that's different: installing the packages through sudo. Unfortunately, this resolved the issue. So it appears to be a permissions issue and not a problem with yarn/pm2 itself.
It's also worth mentioning that i installed pm2 as global using npm, not yarn.
I run my commands using sudo in fork mode. Example:
sudo pm2 start yarn --name "App Name" -x -- dev

I'll probably take the time to figure out the proper way to set the permissions at some point later. But this is a quick and dirty workaround i wanted to share. (Not Recommended).

EDIT:
I tried to install everything again with the proper permissions. This time i succeeded and was able to run yarn through pm2 without using sudo. I identified the potential difference as installing yarn through npm rather than installing it as an independent package (i.e. npm install yarn -g instead of installing it through dpkg). Also, installed PM2 through npm as well (npm install pm2 -g). Then run yarn through pm2 in fork mode (i.e. pm2 start yarn --name "app name" -x -- dev )

Here is some additional information if it's any help:

Versions:
OS: Ubuntu 18.04 LTS
Yarn: 1.7.0
Node: v10.3.0
npm: 6.1.0
pm2: 2.10.4

Installation Methods:
Node (through the debian package):
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs

pm2 (globally through npm):
sudo npm i pm2 -g

yarn (globally through npm)
sudo npm i yarn -g

installing the project packages (locally through yarn):
sudo yarn install

running yarn (through pm2 in fork mode):
pm2 start yarn --name "app name" -x -- dev

This is everything i think i did to get it to work.

what I found out is using yarn.js instead of yarn works better and should be used

https://github.com/yarnpkg/yarn/issues/3405

Got the same issue out of the blue, changing my conf to this worked for me:

{
      "name"        : "my_app",
      "script"      : "yarn run server",
      "args"        : "",
      "watch"       : true,
      "node_args"   : "",
      "merge_logs"  : true,
      "cwd"         : "/apps/www/api.com",
      "env": {
        "NODE_ENV": "production"
      }
    }

this is what I had initially

{
      "name"        : "my_app",
      "script"      : "yarn",
      "args"        : "run server",
      "watch"       : true,
      "node_args"   : "",
      "merge_logs"  : true,
      "cwd"         : "/apps/www/api.com",
      "env": {
        "NODE_ENV": "production"
      }
    }

```javascript
module.exports = {
apps: [
{
name: 'your-app',
cwd: './path/your-app',
script: 'yarn',
args: 'start',
interpreter: '/bin/bash'
}
]
}

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings