There is a bug when installing yarn with nvm and node version v4.4.4 (using Ubuntu 16.04)
npm install -g yarn
doesn't result in yarn being in the nvm bin directory (for the appropriate version of node)
reproducing:
$ nvm use v4.4.4
...
$ npm install -g yarn
...
$ yarn
The program 'yarn' is currently not installed. You can install it by typing:
sudo apt install cmdtest
yarn
can be found in /home/dev/.nvm/v4.4.4/lib/node_modules/yarn/bin/yarn but not in /home/dev/.nvm/v4.4.4/bin/
_Note_: This isn't a bug with node v6.7 and nvm
This looks like an issue with npm's config variables, as any package installed with npm does not have influence on where it's being installed (post-install scripts could change that but not by default).
Can you run which npm
and npm config get
?
It looks good to me:
$ which npm
/home/ivan/.nvm/versions/node/v4.5.0/bin/npm
$ npm config get
; cli configs
user-agent = "npm/3.10.8 node/v4.5.0 linux x64"
; userconfig /home/ivan/.npmrc
email = "...."
registry = "http://127.0.0.1:5080/"
; node bin location = /home/ivan/.nvm/versions/node/v4.5.0/bin/node
; cwd = /home/ivan/lux/lax
; HOME = /home/ivan
; "npm config ls -l" to show all defaults.
What does npm root -g
print out? This shouldn't be related to yarn at all, unless the "bin" key wasn't defined in yarn's package.json.
It might be an issue with the specific version of npm that node 4.4.4 ships with - if you try npm install -g npm
and then npm install -g yarn
, does it work?
The same issue happens when using nodenv
.
I got exactly the same using nodenv
:
$ which npm
/Users/ilyes512/.nodenv/shims/npm
$ npm config get
; cli configs
user-agent = "npm/3.10.10 node/v6.9.4 darwin x64"
; userconfig /Users/ilyes512/.npmrc
registry = "http://npm.private-registry-was-here.com:4873/"
; node bin location = /Users/ilyes512/.nodenv/versions/6.9.4/bin/node
; cwd = /Users/ilyes512
; HOME = /Users/ilyes512
; "npm config ls -l" to show all defaults.
$ npm root -g
/Users/ilyes512/.nodenv/versions/6.9.4/lib/node_modules
`
$ nodenv global
6.9.4
I was using node 7 and I had to switch to node 6. And when I add global packages like $ yarn global add typings
it will not install it in the default node version, but in the node 7 version.
Seems like yarn is installing globals to ~/.config/yarn/global/node_modules/.bin/
rather than yarn global bin
(~/.nvm/versions/node/vX.X.X/bin
)
Same problem with nodenv using .node-version. I am using Homebrew to manage nodenv and yarn. I am using nodenv to manage nodejs versions.
$ nodenv global
7.0.0
$ nodenv local
7.6.0
md5-cee3cf960afa3498e953d250284db214
$ nodenv prefix
/Users/xxx/.nodenv/versions/7.6.0
md5-cee3cf960afa3498e953d250284db214
$ nodenv versions
7.0.0
* 7.6.0 (set by /Users/xxx/dev/project/local-site/www/web/htdocs/app/.node-version)
md5-cee3cf960afa3498e953d250284db214
$ yarn global bin
/Users/xxx/.nodenv/versions/7.6.0/bin
md5-38cfff140d8470ec8e0fa91dc02abac0
$ yarn global add [packages] --global-folder=`nodenv prefix`
Are we correct to assume that this is a yarn problem? yarn global bin
returns the correct path, it's just installing to what yarn considers to be the global directory if the option isn't provided. Since yarn is reporting the global bin correctly, I'd like to make the case that whatever is being returned by that command, should be the directory in which the packages are installed to, without having to provide the --global-folder
option every time.
Thanks @patrickng for your workaround for nodenv, one way to get it to work also for nvm is to use yarn global bin
:
~
yarn global add @angular/cli --global-folder=yarn global bin
~
@patrickng I agree with you. The docs tell us to add export PATH="$PATH:'yarn global bin'
to our yarn global bin
. I got a bit confused when I would globally add packages and then be unable to run them.
Thanks @gmarziou and @patrickng. I added these to my .zshrc because I hate typing.
alias yga='yarn global add --global-folder=`yarn global bin` '
alias ygr='yarn global remove --global-folder=`yarn global bin` '
Note: yarn global ls
is still messed up for me with this setup, as of yarn 0.22. You can still do
cd `yarn global bin`
yarn outdated
yarn upgrade
And manage your global packages that way.
In my personal setup, I also had a bunch of node modules installed globally pre-yarn and pre-nvm, so I had to excise them all too. A quick way to check is to start your shell with no node selected, and do which mocha
which eslint
etc to see if you have any of these packages installed globally.
Final note, I only wanted to have one version of yarn be actually global (regardless of my current node version), so I don't manage yarn with yarn.
Looks like there are workarounds and it works in general now.
Feel free to open a new issue with a specific repro scenario if something is still broken
This doesn't seem to be working in general for me. I installed Yarn via Homebrew and by default I can't run anything installed globally with yarn global add
.
It seems like I can workaround with --global-folder=`yarn global bin`
, but that hardly seems like an ideal default behavior. Why isn't Yarn installing to the value of npm root -g
?
@justinfagnani, could you open a new issue if repro steps are different from the one in the issue description?
A PR or an investigation what goes wrong with the folders is very much welcome
Once NVM is used, installing yarn after that globally works for me.
npm i -g yarn
. On Mac & Ubuntu. Using Homebrew messes up everything.
So, if you've already installed yarn
using homebrew, uninstall it. Then do an npm i -g yarn
. You're all set after that. Just remember to update yarn from time to time.
EDIT Same method is working for Windows (nvm-windows) as well :)
confirmed.
yarn global add
is not installing packages respectively according to different node versions.
Installing packages into yarn's private bin is not what node.js can anticipate, and it is problematic.
This solved it for me (on Ubuntu 16.04)
export PATH=$PATH:~/.yarn/bin
The Yarn docs show you how to install Yarn via Homebrew, however, they also say _"If you use nvm or similar, you should exclude installing Node.js so that nvm鈥檚 version of Node.js is used."_
I uninstalled the global packages I had previously installed with Yarn, uninstalled Yarn with Homebrew, then reinstalled it using the --without-node flag they suggest like so:
brew install yarn --without-node
After re-installing my global packages via Yarn everything works fine.
I have the following in my dotfiles to help with using yarn and nodenv together: FWIW-
# The following two are required to make yarn install binaries and
# packages in nodenv's location. Without they will go into
# ~/.yarn/bin, ~/.config/yarn. These config settings get written into
# ~/.yarnrc. The problem is that this only works with the `nodenv
# prefix` that is in effect when these commands run (ie. a global
# version) - whereas nodenv can use a local version or one set by
# NODENV_VERSION in the shell
- yarn config set prefix `nodenv prefix`
- yarn config set global-folder `nodenv prefix`
you can use yarn config set prefix ~/.yarn
to set a fixed folder to the executables files, read this yarn doc: Defining install location
Most helpful comment
Seems like yarn is installing globals to
~/.config/yarn/global/node_modules/.bin/
rather thanyarn global bin
(~/.nvm/versions/node/vX.X.X/bin
)