我试图用nvm运行另一个NodeJS版本,但得到这个错误:

$ nvm use v4.2.4

nvm is not compatible with the npm config "prefix" option: 
   currently set to "/Users/z/.npm-global"
Run `npm config delete prefix` or `nvm use --delete-prefix v4.2.4` to unset it.

我特意设置了前缀,以避免sudo npm(参见https://docs.npmjs.com/getting-started/fixing-npm-permissions)。

是否有任何方法可以使用nvm而不丢失全局安装包的前缀?


当前回答

对我来说,我去掉了线条

NPM_PACKAGES="${HOME}/.npm-packages"

PATH="$NPM_PACKAGES/bin:$PATH"

从我的.bashrc和能够运行npm install -g package-name,而不需要sudo权限或有错误消息出现在我的shell窗口。

其他回答

我有同样的问题,执行npm config delete prefix没有帮助我。

但这确实起了作用:

使用brew安装nvm后,创建~/。nvm目录: $ mkdir ~/.nvm

并在~/.bash_profile中添加以下行:

export NVM_DIR=~/.nvm
. $(brew --prefix nvm)/nvm.sh

(检查在~/中没有其他与nvm相关的命令。Bashrc或~/。Profile或~/.bash_profile)

打开一个新终端,这次它应该不会打印任何警告消息。 执行nvm——version命令检查nvm是否正常工作。 然后,使用nvm install node && nvm alias default node安装/重新安装NodeJS。

更多信息

我用自制程序安装了nvm,之后我收到了这个通知:

Please note that upstream has asked us to make explicit managing nvm via Homebrew is unsupported by them and you should check any problems against the standard nvm install method prior to reporting. You should create NVM's working directory if it doesn't exist: mkdir ~/.nvm Add the following to ~/.bash_profile or your desired shell configuration file: export NVM_DIR=~/.nvm . $(brew --prefix nvm)/nvm.sh You can set $NVM_DIR to any location, but leaving it unchanged from /usr/local/Cellar/nvm/0.31.0 will destroy any nvm-installed Node installations upon upgrade/reinstall.

忽略它让我看到了这个错误消息:

nvm不兼容npm配置的“prefix”选项:当前设置为“/usr/local/Cellar/nvm/0.31.0/versions/node/v5.7.1” 执行nvm use——delete-prefix v5.7.1——silent命令取消设置。

我遵循了之前的指南(来自homebrew/nvm),之后我发现我需要重新安装NodeJS。于是我做了:

nvm install node && nvm alias default node

它是固定的。

更新: 使用brew安装NVM会导致终端启动缓慢。您可以按照此说明来解决该问题。

如果你把你的主目录挂载在某个地方,这可能是一个问题,因为nvm不能正确地使用符号链接。因为我不关心我的$NVM_DIR位于哪里,我运行这个,一切正常:

$ mv ~/.nvm /tmp/
$ export NVM_DIR="/tmp/.nvm"
$ nvm use --delete-prefix v6.9.1

这可能与您的本地Node安装(如果您过去通过NVM以外的其他方式安装了Node)存在冲突。你应该删除这个node实例:

删除node_modules Sudo rm -rf /usr/local/lib/node_modules 删除节点 Sudo rm /usr/local/bin/node 移除节点link CD /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm .

当你不能安装nvm

I just have a idea. Use the symbolic link to solve the error and you can still use your prefix for globally installed packages. ln -s [your prefix path] [path in the '~/.nvm'] then you will have a symbolic folder in the ~/.nvm folder, but in fact, your global packages are still installed in [your prefix path]. Then the error will not show again and you can use nvm use ** normally. ps: it's worked for me on mac. pps: do not forget to set $PATH to your npm bin folder to use the globally installed packages.

我在把我的主文件夹移动到linux上的新驱动器后遇到了这个问题。通过删除.nvm文件夹并重新安装nvm来修复