我试图用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而不丢失全局安装包的前缀?


当前回答

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.

其他回答

我也有同样的问题,每次使用终端都很烦人。我向终端运行命令,它被修复了

对于那些试图从brew中删除nvm的人

仅仅酿造卸载NVM可能是不够的

如果您看到NPM prefix仍然是/usr/local,请执行此命令

Sudo rm -rf /usr/local/ {lib /节点{/ .npm _modules},垃圾桶,分享/人}/ {npm *,节点*,男士一/节点*}

我有相同的错误消息,但其他解决方案。 在curl (install.sh)期间自动生成的路径不匹配。 检查这个:

echo $NVM_DIR

在我的例子中:/var/www//.nvm。显示在您自动生成的bash文件中,并更改它并替换它:(~/。bash_profile、~ /。zshrc ~ /。配置文件或~/.bashrc)

取代

export NVM_DIR="$HOME/.nvm"

(例如)

export NVM_DIR="$HOME.nvm"

我遵循https://stackoverflow.com/a/47861348/2391795的答案,但效果不太好。

$ npm config delete prefix 
$ npm config set prefix $NVM_DIR/versions/node/v6.11.1

在运行推荐的命令后,我的nvm不再工作,运行nvm use将显示正在使用的正确节点版本,但运行node -v将显示另一个版本。不可能再改变node的版本了。

我完全卸载并重新安装nvm来修复它。我跟随https://github.com/creationix/nvm#manual-uninstall跑步

$ rm -rf "$NVM_DIR"

然后编辑我的.zshrc以删除与nvm相关的行,在我的例子中是

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

然后启动一个新的shell(这样nvm就不会在这个新的shell中加载)并运行https://github.com/creationix/nvm#install-script

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

它添加了nvm我以前在.zshrc中删除的行。

然后我就可以像以前一样使用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.

下面的sh文件有助于我的案例

# nvmish.sh
unset npm_config_prefix
source $HOME/.nvm/nvm.sh
nvm use 18