Using Yarn Global Packages with asdf

October 20, 2019

Anytime I install a global package using yarn, I have issues with being able to find the command at the command line.

When I run this command:

yarn global add <some-package>

And then try to find it at the command line:

which <some-package-command>

I get:

<some-package-command> not found

I know that yarn adds some-package to the global yarn package path. But where is that? And why aren’t commands installed this way found on the command line?

Assuming that whatever path they are installed to is probably not on my $PATH, I run:

yarn global bin

to find where they are being installed. In my specific case, here:

/Users/jennapederson/.asdf/installs/nodejs/8.16.0/.npm/bin.

Whoops! That’s an asdf path! This particular project is using node and I’ve recently introduced asdf into my workflow to manage node (and ruby) versions.

When I look at my .bash_profile or .zshrc file (whichever you’re using), I found these:

. /usr/local/opt/asdf/asdf.sh

. /usr/local/opt/asdf/etc/bash_completion.d/asdf.bash

But nothing about putting asdf package bins on my $PATH.

I can (temporarily) add the following line and re-source:

export PATH="$(yarn global bin):$PATH"

But that’s only going to add the yarn global bin directory that’s in use when it’s sourced, which won’t always be the one I want to use.

I originally installed yarn via Homebrew, which is the yarn recommended way, rather than via npm (as some others recommend as a way to solve this problem).

So instead we’ll update the yarn global path to be outside of asdf and add that to our path.

yarn config set prefix ~/.yarn

This adds a setting to our ~/.yarnrc file (don’t edit this file manually):

prefix "/Users/jennapederson/.yarn"

Then when we do:

yarn global add <some-package>

And try to find it at the command line:

which <some-package-command>

It’s there! And more importantly, it’s not tied directly to the specific node version that asdf is using.