45

I am using NVM and I am trying to install global NPM modules. That action requires permissions to a folder that I don't have permissions to. With regular node.js/npm that was easy to solve with prefix configuration but NVM explicitly forbids it.

What is the right way to change the path to global modules folder in NVM?

Juriy
  • 5,009
  • 8
  • 37
  • 52

2 Answers2

55

To see the location of the current version of node you are using:

nvm which current

You are using the system installation if .nvm is not in the path, similar to the following:

/usr/local/bin/node

To switch to a version managed by nvm:

nvm use 4

To verify you are using a version managed by nvm:

nvm which current

You should see something similar to the following:

/Users/<your-user-name>/.nvm/versions/node/v4.2.2/bin/node

You should only experience global install permission issues when you are using the system installation.

Michael Allan Jackson
  • 4,217
  • 3
  • 35
  • 45
  • is the above solution still relevant nvm which current not works on gitbash or cmd prompt. I have a situation where I installed 8.12.0 using nvm and deleted 8.9.2 which was previously installed without nvm if i check global package list it only shows npm and the path is /c/Program Files/nodejs/npm my previous global packages location /c/Users/Charlie/AppData/Roaming/npm/eslint is there a way to map the old global file location to the newly installed npm (using nvm) – Charles Dominic Sep 21 '18 at 11:57
  • 1
    fixed by adding prefix to .npmrc prefix=C:\Users\Charlie\AppData\Roaming\npm – Charles Dominic Sep 21 '18 at 12:15
  • `nvm use node` instead of `nvm use 4`. – Rodrigo Pinto Oct 09 '19 at 03:26
  • 3
    also useful to know ```export NODE_PATH=$(realpath $(dirname $(nvm which current))/../lib/node_modules)``` – rokstar Mar 28 '21 at 17:54
2

As of 2022, there is no need to change the path to global modules folder over permissions,
as with active nvm node global modules are installed under specific version of node.

Try

$nvm install 16
...
$nvm version > .nvmrc

$ which npm
/Users/user/.nvm/versions/node/v16.14.2/bin/npm
$ which node
/Users/user/.nvm/versions/node/v16.14.2/bin/node

npm global install e.g. npm install -g @angular/cli
will install into
/Users/user/.nvm/versions/node/v16.14.2/lib/node_modules

Remember to nvm use
to get back from .nvmrc to exact node version for which those global modules were installed.

npm list -g --depth=0
to list installed global modules

Paul Verest
  • 60,022
  • 51
  • 208
  • 332