57

On my way setting up Node.js with NVM, I stumbled upon an error when using Yeoman. I got the error

Cannot find module 'yeoman-generator'

After some research I found this post on StackOverflow, which is also about my problem. Now I tried to do so, but the problem I have is, that I want to use different versions of Node.js over the system with the use of NVM. Now is it possible to change the $NODE_PATH dynamically, if the Node.js version changes with the help of NVM? Because my $NODE_PATH is empty at the moment (this is causing the problem).

$ which node
/Users/dschmidt/.nvm/v0.10.35/bin/node
$ which npm
/Users/dschmidt/.nvm/v0.10.35/bin/npm
$ echo $NODE_PATH
[empty]

Would be glad about every answer I get about this. I searched the web for this, but could not find one post about this specifically.

Community
  • 1
  • 1
Nick Schmidt
  • 1,427
  • 1
  • 13
  • 22

4 Answers4

48

Adding following to .bashrc or .zshrc helps

export NODE_PATH=$NODE_PATH:`npm root -g`

I am not expert whether that is good.

source as pointed out by Pensierinmusica

jethar
  • 2,223
  • 2
  • 22
  • 19
  • This is the correct answer specifically for the question asked in regards to working with yeoman generators. – Tortilaman Aug 01 '18 at 19:35
  • Better to use double-quotes around the value, `export NODE_PATH="..."`. If there are spaces anywhere in the paths, double-quotes will save the day. – Gene Pavlovsky Jan 19 '23 at 19:21
  • in my setup, I am happily using node via nvm in one shell, and `npm root -g` produces the correct path. But when I open another shell, `npm root -g` shows that npm is not found. – Ben Wheeler Aug 23 '23 at 13:37
  • also, will this actually be dynamic? or just on shell load? – Ben Wheeler Aug 23 '23 at 13:53
34

NVM will set the path for node and npm once you run

nvm use <node_version>

However, that is just for the current shell and any new shells will not have a version of node an npm selected until your run the previous command unless you set a default version

nvm alias default <node_version>

voila! You have a working version of npm and node in any new shell you open.

To change the default simply run it again with the new version of node you want to use. e.g.

nvm alias default v5.4.0
Radko Dinev
  • 865
  • 7
  • 15
Kosmonaut
  • 2,154
  • 2
  • 18
  • 19
  • I had to drop the backtick marks ` ` for this to work for me. Heads up for other noobs. nvm alias default x.x.x I'm on Linux Mint 18.1 Serena using nvm. – Mike Ferrari Jun 17 '17 at 06:51
  • 4
    Your answer is about general nvm usage. The original query is regarding setting the NODE_PATH variable as per nvm node version (dynamically being the keyword here) – jethar Mar 15 '18 at 06:54
  • 9
    This doesn't seem to be doing the trick. `echo $NODE_PATH` is blank for me after setting this up. – Tortilaman Aug 01 '18 at 19:21
  • 2
    This in fact doesn't answer the question ...nvm alias .. sets the node/npm version that will be available as a command, but it's not setting the $NODE_PATH variable, which is needed by some applications to work correctly :/ – funder7 Apr 17 '20 at 23:19
  • 1
    @funder7 you need to set the $NODE_PATH variable in your .bash_profile or .zshrc or .bashrc as part the installation of nvm – Kosmonaut Jan 22 '21 at 08:50
  • Well I don't think that it's a manual operation, the point of nvm is having node/npm version swapped on your behalf. Nvm installs a nvm.sh script into your shell configuration file, that will handle everything..Too much time has passed so I don't remember now, but reading my comment in the next answer, probably I've solved with `nvm alias default `. Cheers – funder7 Jan 22 '21 at 10:41
  • For me, I had a package whose binary was in: `~/.nvm/versions/node/vx.x.x/lib/node_modules/node_modules/my_package`, so I had to add it to the `PATH` explicitly because it wasn't under the default `~/.nvm/versions/node/vx.x.x/bin`, which is what gets automatically added by NVM. I added it to the `PATH` like so: `export PATH="$HOME/.nvm/versions/node/vx.x.x/lib/node_modules/node_modules/my_package:$PATH"` – GDP2 Jul 28 '21 at 01:58
  • This is no longer working on Windows 11. This answer needs to be updated by someone who knows what they are doing. – John Miller Nov 22 '21 at 15:15
  • I run `nvm install stable --lts && nvm alias default stable` but the selected node version changes every new terminal session – Sh eldeeb Jan 14 '22 at 10:13
22

I figured a way to make this work.

In your ~/.bash_rc file or ~/.zsh_rc add this line:

export NODE_PATH=`which node`

Open new shell or run this source ~/.bash_rc to pick up the changes

Nick
  • 3,581
  • 1
  • 14
  • 36
GN.
  • 8,672
  • 10
  • 61
  • 126
  • 2
    `which node` in zshrc seems to reference system Node. `which $NODE_PATH ` which spits out `/usr/local/bin/node`. Then `/usr/local/bin/node -v` is system Node for me – GN. Aug 03 '16 at 09:39
  • 1
    have you set the version to be used with nvm alias default ? – funder7 Apr 17 '20 at 23:20
  • in my setup, I have nvm setting the node version in one shell, but when I open another, `which node` returns blank. – Ben Wheeler Aug 23 '23 at 13:36
2

This (taken from GN. and Nick) works fine for me, but I needed to add it to ~/.bash_profile because I use Bash as a shell on my Mac

export NODE_PATH=`which node`
RadekR
  • 503
  • 3
  • 10