Yarn: Problems installing packages globally

Created on 10 Apr 2017  ·  8Comments  ·  Source: yarnpkg/yarn

Do you want to request a feature or report a bug?
Bug

What is the current behavior?

I am trying to install d3, but I'm getting a Cannot find module error.

If the current behavior is a bug, please provide the steps to reproduce.

$ yarn global add d3
yarn global v0.22.0
warning No license field
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
warning undefined has no binaries
warning No license field
✨  Done in 2.00s.
$ node
> require('d3')
Error: Cannot find module 'd3'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:339:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:536:10)
>

Also, doing yarn global ls lists no packages.

$ yarn global ls
yarn global v0.22.0
warning No license field
✨  Done in 0.48s.

I installed yarn through homebrew. I tried reinstalling a few times, cleaning /usr/local/Cellar/node/7.8.0/bin and ~/.config/yarn/ beforehand. Nothing seems to help, though.

What is the expected behavior?

I would expect the globally installed module to be available.

Please mention your node.js, yarn and operating system version.

node - v7.8.0
yarn - 0.22.0
hometrew - 1.1.12
macOS - 10.12.2

Most helpful comment

Guys, I've found the solutions.

If you really want to install the module globally and require them in node command then you should set NODE_PATH by add the following line on your ~/.zshrc or ~/.bash_profile.

If you are using yarn

$ export NODE_PATH=$(yarn global dir)/node_modules
# or node
$ export NODE_PATH=$(npm root --quiet -g)

Then try to reload the bash session and require again.
That works on my mac.

Refs:
https://stackoverflow.com/questions/7970793/how-do-i-import-global-modules-in-node-i-get-error-cannot-find-module-module

All 8 comments

This does appear to be a known issue, it's been affecting me too (Mac 10.12.4, Node 7.4.0, Yarn 0.22.0). This thread has a solution that works for me: https://github.com/yarnpkg/yarn/issues/2790

That being, to add the following directory to my PATH:
$HOME/.config/yarn/global/node_modules/.bin
(as the globally installed packages are hiding out there, a location not in your path by default)

I don't expect this to be connected to my PATH variable as I'm not trying to use d3 as an executable. I'm trying to require it as a module.

I don't think it is a good idea to require global modules as they are mostly used as a cli tool.Though, if you really want to, you can expose the global module directory to NODE_PATH env variable so that Node.js knows where to find the module (details).In your case, it is ~/.config/yarn/global/node_modules.

@jddxf ok, I understand. Yeah, I ended up solving the issue for myself by locally installing the d3 module. Just thought it might be good to report it, since I found the behaviour unexpected. Is this something that should be noted in the docs for the yarn global command?

If you expect the globally installed module to be available I would say Yarn has already done that. Even if you're using npm, you'll need to do the same thing to properly require a global module.

Real problem. Not sure what the problem is, so I just use npm install global Lol.

@jddxf The problem is that globally installed modules are not available in REPL by default.
I know 2 valid (not considered as bad practices) use cases of using global modules:

  • CLI
  • REPL

The first is covered by yarn global while the second isn't.

To clarify: I understand that manually modifying NODE_PATH allows using yarn's global modules in REPL. The issue is that yarn is expected to do it by itself by default.

Guys, I've found the solutions.

If you really want to install the module globally and require them in node command then you should set NODE_PATH by add the following line on your ~/.zshrc or ~/.bash_profile.

If you are using yarn

$ export NODE_PATH=$(yarn global dir)/node_modules
# or node
$ export NODE_PATH=$(npm root --quiet -g)

Then try to reload the bash session and require again.
That works on my mac.

Refs:
https://stackoverflow.com/questions/7970793/how-do-i-import-global-modules-in-node-i-get-error-cannot-find-module-module

Was this page helpful?
0 / 5 - 0 ratings