Node.js - Configuring $NODE_PATH with NVM

社会主义新天地 提交于 2019-11-30 21:38:28

问题


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.


回答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




回答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



回答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



来源:https://stackoverflow.com/questions/27876557/node-js-configuring-node-path-with-nvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!