Node.js - Configuring $NODE_PATH with NVM

47,036

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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`
Share:
47,036

Related videos on Youtube

Nick Schmidt
Author by

Nick Schmidt

Interface designer, Thinker, Ruby on Rails developer, coffee drinker.

Updated on July 09, 2022

Comments

  • Nick Schmidt
    Nick Schmidt almost 2 years

    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.

    • laggingreflex
      laggingreflex over 9 years
      $ export NODE_PATH=whatever
    • Nick Schmidt
      Nick Schmidt over 9 years
      I know this, I mentioned dynamically.
    • Roger Lam
      Roger Lam about 9 years
      Also looking for what others did. Did you find a solution @DominikSchmidt?
    • Nick Schmidt
      Nick Schmidt about 9 years
      I fixed it by just installing Node without NVM. But didn't find a solution for this with the use of NVM.
    • Pensierinmusica
      Pensierinmusica about 9 years
      Does this help?
  • GN.
    GN. over 7 years
    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
  • Mike Ferrari
    Mike Ferrari almost 7 years
    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.
  • jethar
    jethar about 6 years
    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)
  • Tortilaman
    Tortilaman over 5 years
    This doesn't seem to be doing the trick. echo $NODE_PATH is blank for me after setting this up.
  • Tortilaman
    Tortilaman over 5 years
    This is the correct answer specifically for the question asked in regards to working with yeoman generators.
  • funder7
    funder7 about 4 years
    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
    funder7 about 4 years
    have you set the version to be used with nvm alias default <version> ?
  • Kosmonaut
    Kosmonaut over 3 years
    @funder7 you need to set the $NODE_PATH variable in your .bash_profile or .zshrc or .bashrc as part the installation of nvm
  • funder7
    funder7 over 3 years
    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 <version>. Cheers
  • GDP2
    GDP2 almost 3 years
    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"
  • John Miller
    John Miller over 2 years
    This is no longer working on Windows 11. This answer needs to be updated by someone who knows what they are doing.
  • Sh eldeeb
    Sh eldeeb over 2 years
    I run nvm install stable --lts && nvm alias default stable but the selected node version changes every new terminal session