99

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.

Aurora0001
  • 13,139
  • 5
  • 50
  • 53
jleeothon
  • 2,907
  • 4
  • 19
  • 35

8 Answers8

104

Simply type

yarn global add nodejs


yarn global add nodejs mysql mongodb
vijay
  • 10,276
  • 11
  • 64
  • 79
57

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.
Community
  • 1
  • 1
Aurora0001
  • 13,139
  • 5
  • 50
  • 53
  • 3
    Thanks for your response! Sure, it makes sense to me that yarn adopts some local-first kind of thinking because in the end, that's how every grown-up developer needs to use their dependencies. My use case—elaborating on it—was I'm making a docker image that is used to lint a project as part of a (GitLab) CI step. So I don't really need isolation because the sole purpose of this image is to use the `eslint.js` executable. – jleeothon May 10 '17 at 21:01
  • 95
    I think you're being a little too strict there. There are a good number of command-line tools that are not tied to a project, but rather my shell environment and those are absolutely best served as a global installation. – Tommi Kyntola Apr 19 '18 at 05:55
  • 2
    I agree with Tommi on this one. While usually a bad idea, something like PM2 usually gets installed globally. – Ron Apr 24 '18 at 14:41
  • Of course—"you don't" is aimed at the question's suggestion of installing a batch of packages globally. I'm not saying that you *can't ever* install a package globally. There are limited cases where installing a package globally is useful, which is why the functionality is there (albeit with a little friction). – Aurora0001 Apr 24 '18 at 15:44
  • 1
    @Aurora0001 actually yes I do. For a similar purpose as OP. My isolation is my docker image. It's build and runs and no one needs to know how the files are distributed inside of it. They should look at the source instead. However since I want to mount my source files into the image, so the container recompiles with every save and I don't want to mix the node modules on my machine and those in the container (different) platforms. Installing from package.json but globally would make it easier to mount the folders required and I would loose isolation or discoverability – Rune FS Aug 30 '18 at 11:53
  • The docs give an example of installing create-react-app globally here: https://yarnpkg.com/en/docs/cli/global – Mark Wilbur Sep 11 '19 at 10:18
  • project based installation makes sense however as Tommi said. there are tools which you wish to use as standard and available like other usr/bin toolings, i.e. vue-cli, bats test etc. – mirageglobe May 09 '20 at 13:48
16

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.

jadelord
  • 1,511
  • 14
  • 19
laurent
  • 88,262
  • 77
  • 290
  • 428
6

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.

Dr-Bracket
  • 4,299
  • 3
  • 20
  • 28
  • in my case i need to run Detox, but when run with `yarn dlx detox ` it fails because it can't then find `jest` which it would do if ran with the global install, I am trying to see what I can do but for now, not liking the new version of yarn. – Carlos Valencia Jul 18 '23 at 14:29
2

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

Nsikak
  • 21
  • 1
2

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)

Nand
  • 611
  • 6
  • 12
1

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.

-7

npm install -g markdown-toc

Yarn decided not to support this functionality.

B T
  • 57,525
  • 34
  • 189
  • 207
  • 1
    1. The question is NOT complete with the title alone; 2. yarn support install globally. – InQβ Aug 14 '19 at 01:07