How to install a list of many global packages with Yarn

yarn install -h suggests that the -g (global) option is DEPRECATED. How am I supposed to indicate that I want a bunch of packages (from package.json / yarn.lock files) to be installed globally?

Options I saw:

  • yarn global [command] has things such as ls and add but not install. add only works with particular package names, if I understand correctly. I already have my yarn.lock file ready, I don't want to repeat myself on the command line.
  • yarn global add each package one by one. Now my list of packages would be imperative instead of declarative.

Specifically, I'd like to use one executable from one of those packages.

87632 次浏览

How am I supposed to indicate that I want a bunch of packages (from package.json / yarn.lock files) to be installed globally?

You shouldn't. Installing globally is discouraged by Yarn, and there are very few situations where it's necessary, or even helpful.

As noted in the documentation:

For the vast majority of packages it is considered a bad practice to have global dependencies because they are implicit. It is much better to add all of your dependencies locally so that they are explicit and anyone else using your project gets the same set of dependencies.

If you are trying to use a CLI tool that has a bin you can access these in your ./node_modules/.bin directory.

But I really, really want to!

If you really don't want to listen to the advice given, use

yarn global add <package>

However, don't expect to easily install a huge list of dependencies globally—it's hard to do by design, because it's not a good idea.


Instead, the intended flow with Yarn is:

  • install everything locally, so each project is isolated
  • call binaries from ./node_modules/.bin where possible
  • avoid global installs—they're a convenience, but not one you should rely on.

For those interested, here's a way to install and manage global applications installed via yarn.

First create a directory which will contain the applications, for example ~/.yarn-global:

mkdir ~/.yarn-global
cd ~/.yarn-global

Then install your application from here:

yarn add yourapp

Finally open your profile file, i.e. .bashrc or .bash_profile and add the path to the bin directory:

export PATH="$PATH:$HOME/.yarn-global/node_modules/.bin"

From now on, any application you install in this directory will be available from anywhere in your shell.

Once this is done, you can even create a yarn-global utility script that will only operate in this .yarn-global directory. For example:

sudo vim /usr/bin/yarn-global
sudo chmod 755 /usr/bin/yarn-global

And the script content would be:

#!/bin/bash
cd "$HOME/.yarn-global"
yarn $1 "$2"

Now you can do yarn-global add someapp, yarn-global upgrade someapp, etc.

Simply type

yarn global add nodejs

npm install -g markdown-toc

Yarn decided not to support this functionality.

Yarn adds all global package to a .yarn folder in your home: ~/.yarn/bin

so you have to export it as:

export PATH="$PATH:$HOME/.yarn/bin"

or add it to the .bashrc file in your home folder

As containerization becomes the norm for application development locally AND for deployment to each environment, these conventions become less relevant. Each container image is its own server, and you can share the entire development environment with other developers using a Dockerfile and docker-compose (or other methods to containerize an application). Using this strategy, I can develop on the exact environment that gets deployed to prod, e.g. OS, libraries, versions, etc. But, developing a containerized application on a Mac or Windows machine requires a Virtual Machine + local host mounted directory to VM and then docker volume mounts from the VM. This runs terribly slowly when node_modules is part of that volume mount. Why can a developer not choose to do something that isnt 'recommended' if it improves their development experience? I think this should be revisited.

For Global yarn install just type

yarn global add nodejs

Make sure that you already have Node installed on your system. Using this above command the yarn packages can be installed globally.

Note - "install" has been replaced with "add" now (this is for packages only)

Yarn 2, in its infinite wisdom, has removed yarn global.

If you want to run a package, like firebase-tools, you now do it via yarn dlx firebase-tools <parameters>, instead of installing it then calling the installed package.

This is a terrible decision and I hope Yarn reverts it.