Javascript Node.js - 使用 NVM 配置 $NODE_PATH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27876557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:50:12  来源:igfitidea点击:

Node.js - Configuring $NODE_PATH with NVM

javascriptnode.jsnpmnvm

提问by Nick Schmidt

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

在设置 的过程Node.jsNVM,我在使用Yeoman. 我得到了错误

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).

经过一番研究,我在 StackOverflow 上找到了这篇文章,这也是关于我的问题。现在我尝试这样做,但我遇到的问题是,我想通过使用 NVM 在系统上使用不同版本的 Node.js。如果 Node.js 版本在 NVM 的帮助下发生变化,现在是否可以动态更改 $NODE_PATH?因为我的 $NODE_PATH 目前是空的(这是导致问题的原因)。

$ 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.

我会很高兴我得到的每一个答案。我在网上搜索了这个,但找不到专门关于这个的帖子。

回答by jethar

Adding following to .bashrcor .zshrchelps

添加以下内容.bashrc.zshrc帮助

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

I am not expert whether that is good.

我不是专家,这是否好。

sourceas pointed out by Pensierinmusica

通过Pensierinmusica指出

回答by Kosmonaut

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

运行后,NVM 将为 node 和 npm 设置路径

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

但是,这仅适用于当前 shell,除非您设置默认版本,否则任何新 shell 都不会选择 npm 的节点版本,除非您运行上一个命令

nvm alias default <node_version>

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

瞧!在您打开的任何新 shell 中,您都有一个 npm 和 node 的工作版本。

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

回答by GN.

I figured a way to make this work.

我想出了一种方法来完成这项工作。

In your ~/.bash_rcfile or ~/.zsh_rcadd this line:

在您的~/.bash_rc文件中或~/.zsh_rc添加以下行:

export NODE_PATH=`which node`

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

打开新的 shell 或运行它source ~/.bash_rc以获取更改