jleeothon
jleeothon

Reputation: 3146

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:

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

Upvotes: 104

Views: 124271

Answers (8)

vijay
vijay

Reputation: 10997

Simply type

yarn global add nodejs


yarn global add nodejs mysql mongodb

Upvotes: 114

Dr-Bracket
Dr-Bracket

Reputation: 5494

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.

Upvotes: 8

Nand
Nand

Reputation: 803

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)

Upvotes: 2

Darin London
Darin London

Reputation: 37

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.

Upvotes: 1

Nsikak
Nsikak

Reputation: 21

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

Upvotes: 2

Aurora0001
Aurora0001

Reputation: 13547

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.

Upvotes: 58

B T
B T

Reputation: 60875

npm install -g markdown-toc

Yarn decided not to support this functionality.

Upvotes: -7

laurent
laurent

Reputation: 90776

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.

Upvotes: 17

Related Questions