Yarn: Possible to set the global install path

Created on 11 Oct 2016  Â·  29Comments  Â·  Source: yarnpkg/yarn

This is a feature (or perhaps documentation request).

When I run the following:

yarn global add create-react-app

Yarn will try to symlink into /usr/bin which requires sudo. With NPM, I've set my global packages to install into a user directory that is then added to my $PATH.

Is it possible to set a global directory via config somehow?

I've tried the following to no avail:

yarn add create-react-app -g --global-folder ~/bin (installs into ~/node_modules)
yarn global add create-react-app --global-folder ~/bin (still goes for /usr/bin)

This is all running on Debian 8.5

Most helpful comment

It seems that yarn config set prefix /usr/local/ works for me. Now when I do yarn global add X it creates the package in /usr/local/bin/X

All 29 comments

It's not currently supported but we can definently add an option.

This is a really important feature and a must have IMO.

So +1.

If you install node to a user-owned location and set your own PATH, this already appears to work.

I always unpack node to a place like ~/Library/node-v6.7.0-darwin-x64, symlink that to ~/Library/node, and then add ~/Library/node/bin to PATH. Under those conditions, yarn correctly puts globals in the same place npm does, and sudo is not required.

This feature is very helpful for people who are unable to (or merely inclined against) running package installation operations, which sometimes download and execute staggering amounts of code of questionable provenance, as root.

The npm option here is --prefix, so although it's very not recommended to use it, it'd be good to keep the same terminology.

Rather than pass an option to npm, I have prefix=${HOME}/.local in my ~/.npmrc. It'd be great if yarn respected that (and other config settings), so that it could a true drop-in replacement for npm.

Notice that you can already add ~/.yarn-cache/.global/node_modules/.bin/ (aka what is outputed by yarn global bin) to your PATH, the only problem is the linking to /usr/bin that should be easy to disable!

Notice that you can already add (…) to your PATH

Instead of adding another directory to my PATH (which then means I’d have to do so in every other tool as well) I’d rather be able to set an option to link the binaries to wherever I want at install time.

is this going to be fixed or is this a wont fix. its seems to be one of the most requested bugs.

related ones: #1389, #1388, #1321, #1226, #1194, #1151, #1060, #1029, #927 (comment), #630, #1370

1600 might also be related

Created #1654 to fix this, pending approval.

Linked PR makes it possible to do:

$ yarn global add react-native-cli --prefix /my-custom-prefix

Linked PR makes it possible to do:

$ yarn global add react-native-cli --prefix /my-custom-prefix

What would be the correct way to make such a prefix permanent? Is there a line we can add to .bashrc (or whatever)?

It should get the default npm path:

npm config get prefix

You can run this insted

yarn global add react-native-cli --prefix $(npm config get prefix)

@tadeugr If your comment was in reply to my question, it doesn’t answer it. The question was about making a custom path the default without having to continuously use --prefix.

The answer is export PREFIX='/my-custom-prefix' in your shell’s startup files. Can’t help but think PREFIX is awfully generic and we should have something else.

@vitorgalvao you can play around with it. For example, I put this in my .bashrc file:

export prefix=$(npm config get prefix)
export NODE_PATH=$(npm root -g)

Any news on your question @vitorgalvao?

@vitorgalvao Oh, I thought that was a wish, not the answer. Thank you.

The answer is export PREFIX='/my-custom-prefix' in your shell’s startup files.

having to define PREFIX as a global environment variable is absolutely not a solution, because that variable used by many programs to define other things, for example make.

having to define PREFIX as a global environment variable is absolutely not a solution

It absolutely is a solution. It deliberately works! Is it a good solution? No. But I mentioned that in the next sentence:

Can’t help but think PREFIX is awfully generic and we should have something else.

I don’t think this is the place to have that discussion, though. For all intents and purposes this issue is solved and closed. Having a better global variable should be its own specific issue.

It seems that yarn config set prefix /usr/local/ works for me. Now when I do yarn global add X it creates the package in /usr/local/bin/X

Thank you @TimvdLippe. Definitely the right approach.

@TimvdLippe thanks, this worked. But if you have some pre-installed packages from npm in the folder, yarn global ls doesn't list them. Anybody having the same issue?

@bastilimbach That's expected behaviour. Yarn is looking somewhere else for global packages once you changed the place where global packages get installed. Thanks @TimvdLippe, that was definitely the solution we were looking for!

@nachoaIvarez I fixed it by removing all packages and adding them back with yarn global add.

@TimvdLippe

It seems that yarn config set prefix /usr/local/ works for me. Now when I do yarn global add X it creates the package in /usr/local/bin/X

not works with 0.21.3 on macOS

@TimvdLippe
windows 7 x64

After setting the prefix it does work but the actual node_modules are still installed in AppData\Local\Yarn\config\globalnode_modules. The prefix folder just contains the .cmd files. Is there anyway to change the actual install directory?

This is the only reason why I have not switch to yarn, I need to install my front end packages in a specific folder.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ocolot picture ocolot  Â·  3Comments

sebmck picture sebmck  Â·  3Comments

NonPolynomial picture NonPolynomial  Â·  3Comments

FLGMwt picture FLGMwt  Â·  3Comments

chiedo picture chiedo  Â·  3Comments