Nvm: npm does not list globally installed modules

Created on 7 Dec 2016  ยท  20Comments  ยท  Source: nvm-sh/nvm

Hi,

npm list -g is not showing globally installed modules:

# adam.moss@scarecrow:~ $ npm list -g
/Users/adam.moss/.nvm/versions/node/v7.2.1/lib
โ””โ”€โ”€ (empty)

However there are modules installed (inc. npm itself, obviously ๐Ÿ˜ƒ)

# adam.moss@scarecrow:~ $ ls .nvm/versions/node/v7.2.1/lib/node_modules/
total 0
drwxr-xr-x  10 adam.moss  staff   340B  7 Dec 14:07 .
drwxr-xr-x   4 adam.moss  staff   136B  6 Dec 21:48 ..
drwxr-xr-x  12 adam.moss  staff   408B  7 Dec 14:07 gulp-cli
drwxr-xr-x   9 adam.moss  staff   306B  7 Dec 14:07 istanbul
drwxr-xr-x  10 adam.moss  staff   340B  7 Dec 14:07 jasmine
drwxr-xr-x  15 adam.moss  staff   510B  7 Dec 14:07 mocha
drwxr-xr-x  27 adam.moss  staff   918B  7 Dec 14:07 npm
drwxr-xr-x  21 adam.moss  staff   714B  7 Dec 14:07 pm2
drwxr-xr-x   6 adam.moss  staff   204B  7 Dec 14:07 react-native-cli
drwxr-xr-x  21 adam.moss  staff   714B  7 Dec 14:07 snyk

Debug output

# adam.moss@scarecrow:~ $ nvm debug
nvm --version: v0.32.1
$SHELL: /usr/local/bin/bash
$HOME: /Users/adam.moss
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: v7.2.1
which node: $NVM_DIR/versions/node/v7.2.1/bin/node
which iojs: 
which npm: $NVM_DIR/versions/node/v7.2.1/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v7.2.1
npm root -g: $NVM_DIR/versions/node/v7.2.1/lib/node_modules

And npm outdated -g does actually work

# adam.moss@scarecrow:~ $ npm outdated -g
Package  Current   Wanted  Latest  Location
npm      3.10.10  3.10.10   4.0.3

Contents of my .npmrc

save-exact=true

This is on macOS Sierra (v10.12.1)

needs followup

Most helpful comment

@vitaly-t the command is npm list -g --depth=0 - note the double dashes.

All 20 comments

When this happens, it's a bug in your node installation. The only solution I've found is to completely uninstall that version of node, and reinstall it. It's also possible it's a permissions issue (hopefully you never used sudo with any node/npm commands, especially when using nvm).

Yeah I've tried the install/uninstall, including a brute force rm -rf .nvm. No joy.

Definitely no sudo ๐Ÿ‘

Actually after some more digging I think this is a npm bug. I have NODE_ENV=production in my environment. If I remove that it works as expected. So digging through ls.js in npm itself it appears something is going wrong between lines 133 and 145.

That makes sense - having NODE_ENV globally set to production in a non-production environment seems like it could be problematic.

See https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders .

For me, this solved it:

ln -s ~/.nvm/versions/node/v6.9.2/lib/node_modules ~/.node_modules

However, I think this should be solved within NVM properly.

@geekdenz that's a very bad idea. global modules should never be shared across node versions - when installed, they may make all kinds of decisions that are specific to the node version: which syntax is supported, which shims are needed, they might compile C++ code, etc. The way nvm handles it properly is requiring that you have a separate global install list for every single version.

However, this isn't related to this issue, which is caused by a broken npm installation.

@ljharb As I said, this is not a real solution, it is a temporary work-around. I thought that was clear from the comment:
However, I think this should be solved within NVM properly.

Totally! I'm saying that there's no way nvm can actually solve it - the solution when a node version's npm is corrupted is to nvm uninstall it and reinstall it.

Hang on! So, how do I solve this issue then properly?

@geekdenz nvm deactivate && nvm uninstall 6.9.2 && nvm install 6.9.2

Thanks. That looked like it should work, but it doesn't:

nvm deactivate && nvm uninstall 6.9.2 && nvm install 6.9.2
/home/denz/.nvm/*/bin removed from $PATH
/home/denz/.nvm/*/share/man removed from $MANPATH
N/A version is not installed...
VERSION_PATH=''
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v6.9.2 (npm v3.10.9)
โžœ denz@saturn  ~  npm i -g exec                                             
npm WARN deprecated [email protected]: deprecated in favor of builtin child_process.execFile
/home/denz/.nvm/versions/node/v6.9.2/lib
โ””โ”€โ”€ [email protected] 

โžœ denz@saturn  ~  node                                                      
> var exec = require('exec')
Error: Cannot find module 'exec'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at repl:1:12
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:313:29)
    at bound (domain.js:280:14)

Sorry, but it looks like this doesn't work with the latest stable version of nvm.

You can't require global modules in any version of node, unless NODE_PATH is set, and you should never do that. If you require something, it's a dependency, and must be installed locally. Global modules are for command-line executable binaries.

OK. Thanks for letting me know.

How do I write a command-line executable then with NODE_PATH set?
What do I need to set NODE_PATH to sing NVM?

You don't set NODE_PATH at all, that's the point :-)

Please open a new issue if you have further questions; we've gotten off topic of this issue.

OK Thanks.

This issue should be re-opened, it is not gone, and I can't solve it. Re-installation doesn't help.

OS: Windows 10, 64-bit
Node.js: 7.4.0

If I do this:

npm list -g

i'm seeing a list of modules, but if I do this:

npm list -g depth=0

then the list comes up empty, even though I have a few global modules installed.

@vitaly-t the command is npm list -g --depth=0 - note the double dashes.

@ljharb oh, stupid me,...thanks so much for pointing out at the obvious, I was being blind, it seems, it does indeed work that way.

@ljharb @vitaly-t but, the command npm list -g --depth=0 will show all package has been installed.Is there hava some other command ?

@posebear1990 no, that's the only command. If that works, nvm will work. If not, npm is broken, and you need to reinstall node.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

raitucarp picture raitucarp  ยท  3Comments

ssbothwell picture ssbothwell  ยท  4Comments

watson picture watson  ยท  5Comments

goalidea picture goalidea  ยท  3Comments

nickngqs picture nickngqs  ยท  3Comments