`sh: yarn: command not found` in Mac task runner

Just to be clear before proceeding to word out my issue: This is not the terminal I'm talking about. My webstorm's terminal works just fine.

 

Environment: Webstorm

Version: 2017.3

Platform: Mac Sierra

 

I keep getting yarn is not found whenever I run npm tasks from the configurations/npm window.

 

My package.json's scripts:

    "build": "...",
    "serve": "..."
    "start": "yarn run build && yarn run serve"
    ...

 

Whenever I try double clicking `start` from the `npm window` I get `sh: yarn: command not found` error, same from the configuration window. But everything works as expected from the IDE's terminal. So its not same issue that others have where terminal is not inheriting bash. My problem here is that the Task Runner is not inheriting bash instead. I tried echoing out $PATH in npm scripts:

Bash:

/Users/ME/.yarn/bin:/Users/ME/.nvm/versions/node/v6.12.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

IDE Terminal:

/Users/ME/.yarn/bin:/Users/ME/.nvm/versions/node/v6.12.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Task Runner:

/Users/ME/.nvm/versions/node/v6.12.3/lib/node_modules/npm/bin/node-gyp-bin:/Users/ME/Development/PROJECT/node_modules/.bin:/Users/ME/.nvm/versions/node/v6.12.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

 

As we can see the `~/.yarn/bin` is not getting loaded inside the Task Runner.

My .bash_profile:

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    export PATH="$HOME/.yarn/bin:$PATH"

I also tried adding a ~/.profile file with `. ~/.bash_profile` and restarted IDE to no success.

Also, since the `npm run` command works, only `yarn run` command didn't work, I thought the `~/.bash_profile` itself was not loaded and to confirm I tried executing `nvm list` from the script, which also failed with `sh: nvm: command not found` error.

Please help me to resolve this issue.

1
5 comments

Must be a $PATH issue. Can you check if the problem persist when running WebStorm from terminal (open -a /Applications/webstorm.app )?

on MacOSX the environment variables differ between GUI applications and within the terminal. Terminal environment is only available to applications started from terminal. To solve this problem, Webstorm tries to load terminal environment by executing the following command on startup:

<your shell> -l -i -c '/Applications/webstorm.app/bin/printenv.py'

Seems this command can't retrieve all needed stuff in your case - thus the issue.

Some links you may find useful: http://apple.stackexchange.com/questions/106355/setting-the-system-wide-path-environment-variable-in-mavericks, https://devnet.jetbrains.com/docs/DOC-1160#comment-2801, http://apple.stackexchange.com/questions/51677/how-to-set-path-for-finder-launched-applications.. The problem is that the way to define system-wide environment variables on Mac changes from one version to another (even minor system updates may break your environment)

0
Avatar
Permanently deleted user

@Elena, Thank you for your reply and apologies for my delayed reply, I was trying out your suggestions and links. `open -a Webstorm.app` does indeed solve the problem, thank you, but I don't want to do it every single time I want to launch the application, so, I followed the links, many of them are outdated, and the devnet was a dud, but the "how to set path for finder launched applications" link showed me I can set env through `lanuchctl`:

launchctl setenv PATH /my/new/path:$(launchctl getenv PATH)

This worked like a charm. But it gave an dirty PATH env so I wrote a small script for adding yarn to the finder env:

YARN="$HOME/.yarn/bin"
LPATH=$(launchctl getenv PATH)

export PATH="$YARN:$PATH"
if [ -z "$LPATH" ]; then
launchctl setenv PATH "$YARN:$LPATH"
else
launchctl setenv PATH "$YARN"
fi

For anybody comes across this issue, please add this to your .bash_profile file and make sure restart the Dock/Finder to get the settings going:

$ killall Dock

Thank you!

0
Avatar
Permanently deleted user

@Elena I guess I was wrong in assuming the above solution worked. It worked because I did `killall Dock` in terminal which restarted terminal with bash's current env settings which included the yarn path. But once the system is restarted or if I restart the Dock from ActivityMonitor then it doesn't pick up the envs and get the same error. So the solution is not complete. Do you have any other solution that can be applied here? IMO this should work out of the box or have a way to let the developers set some environment variables explicitly (at least) for the IDE. If that feature is already there, can you please direct me to do so?

0

>IMO this should work out of the box

 

Sounds good, but it not that easy. From the links I have provided you can probably see that this is not WebStorm-specific issue.

as I wrote in my very first comment, WebStorm tries to load your environment from shell on startup, running

<your shell> -l -i -c '/Applications/webstorm.app/bin/printenv.py'

this script either fails, or doesn't retrieve all environment vars for some reason

Please provide your idea.log (https://intellij-support.jetbrains.com/hc/en-us/articles/207241085-Locating-IDE-log-files) - don't paste its content here, put it on some file server

1

I'm facing that stupid issue every time I setup my new mac or environment. Why it doesn't work out of the box?

WHY when I setup package manager path it doesn't work either? What excuse you will give that time?

result:

What helped me - is to uninstall yarn and install it with brew https://stackoverflow.com/a/54927249/6548013

0

Please sign in to leave a comment.