Node.js - Configuring $NODE_PATH with NVM
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`
Related videos on Youtube
Nick Schmidt
Interface designer, Thinker, Ruby on Rails developer, coffee drinker.
Updated on July 09, 2022Comments
-
Nick Schmidt almost 2 years
On my way setting up
Node.js
withNVM
, I stumbled upon an error when usingYeoman
. I got the errorCannot 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 over 9 years
$ export NODE_PATH=whatever
-
Nick Schmidt over 9 yearsI know this, I mentioned dynamically.
-
Roger Lam about 9 yearsAlso looking for what others did. Did you find a solution @DominikSchmidt?
-
Nick Schmidt about 9 yearsI fixed it by just installing Node without NVM. But didn't find a solution for this with the use of NVM.
-
Pensierinmusica about 9 yearsDoes this help?
-
-
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 almost 7 yearsI 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 about 6 yearsYour 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 over 5 yearsThis doesn't seem to be doing the trick.
echo $NODE_PATH
is blank for me after setting this up. -
Tortilaman over 5 yearsThis is the correct answer specifically for the question asked in regards to working with yeoman generators.
-
funder7 about 4 yearsThis 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 about 4 yearshave you set the version to be used with nvm alias default <version> ?
-
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 over 3 yearsWell 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 almost 3 yearsFor 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 thePATH
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 thePATH
like so:export PATH="$HOME/.nvm/versions/node/vx.x.x/lib/node_modules/node_modules/my_package:$PATH"
-
John Miller over 2 yearsThis is no longer working on Windows 11. This answer needs to be updated by someone who knows what they are doing.
-
Sh eldeeb over 2 yearsI run
nvm install stable --lts && nvm alias default stable
but the selected node version changes every new terminal session