9

Does it matter whether you install a global package with yarn global add PACKAGE vs npm install -g PACKAGE ?

Is there any difference at all, like where files are installed? If yes, what is it?

Andreas
  • 2,287
  • 2
  • 23
  • 26
  • Is this a generic question or you have a specific problem which is happening with a difference between the two? – Tarun Lalwani Jul 13 '19 at 16:15
  • 2
    I suspect I have troubles with some global packages behaving differently with installing with yarn vs npm. But I can't say for sure. That's why I'm asking. – Andreas Jul 14 '19 at 11:56
  • Could you please clarify the name of few packages for which you are facing issues? – VAT Jul 19 '19 at 15:04

2 Answers2

6

So yes, you are right it is different. For npm it is something like below

/Users/tarunlalwani/.nvm/versions/node/v9.2.0/lib if you are using nvm

You can get this path using

$ npm config get prefix
/Users/tarunlalwani/.nvm/versions/node/v9.2.0

Where does npm install packages?

While yarn uses other paths

  • Windows: %LOCALAPPDATA%/Yarn/config/global
  • OSX and Linux non-root: ~/.config/yarn/global
  • Linux if logged in as root: /usr/local/share/.config/yarn/global

How to display yarn globally installed packages?

See this thread as well

https://github.com/yarnpkg/yarn/issues/2049

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 1
    So does this path difference mean with Yarn the global packages are not associated with your NVM version of Node? Meaning you should use `npm` instead of `yarn` for global packages if you use NVM? – Alec Rust May 19 '21 at 15:07
  • @AlecRust `nvm` installs yarn underneath its version controlled directory. So if you print out `which yarn` it would be installed in a directory like `/Users/{username}/.nvm/versions/node/{version}/bin/yarn`. Hence no problem with global installations on either `npm` or `yarn` even when using `nvm` – Aningaaq Aug 22 '22 at 07:03
-1

This is the document about Yarn global

yarn global is a prefix used for a number of commands like add, bin, list and remove. They behave identically to their normal versions except that they use a global directory to store packages. The global command makes executables available to use on your operating system

and this is the document about npm install global mode

In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.

I think there is no difference between them. Install a package as a global useful for developer tooling that is not part of any individual project but instead is used for local commands

Vu Luu
  • 792
  • 5
  • 16