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


当前回答

以下是我所做的工作

1- sudo su

2-root@antonio:/home/antonio# mv ~/.nvm /tmp/

3- root@antonio:/home/antonio# export NVM_DIR=“/tmp/.nvm”

4- root@antonio:/home/antonio# NVM使用——delete-prefix v17.4.0

5- ' root@antonio:/home/antonio# node -v

`

其他回答

我解决了这个问题,当它显示在VSCode和JetBrains终端,但不是在本机终端使用以下命令:

ls -la /usr/local/bin | grep "np[mx]"

这将为你提供最终的解析路径:

... npm -> ../lib/node_modules/npm/bin/npm-cli.js
... npx -> ../lib/node_modules/npm/bin/npx-cli.js

从那里,删除文件并重新启动VS Code应该可以修复这个问题:

rm -R /usr/local/bin/npm /usr/local/lib/node_modules/npm/bin/npm-cli.js
rm -R /usr/local/bin/npx /usr/local/lib/node_modules/npm/bin/npx-cli.js

修复链接:https://github.com/nvm-sh/nvm/issues/1690#issuecomment-392014774

我也遇到过同样的问题,这是因为node和nodejs的npm冲突

因此,通过以下命令删除nodejs

sudo apt-get remove nodejs

这将删除所有与nodejs相关的包,

在上面的命令之后,您必须运行autoremove命令

sudo apt autoremove

这样问题就解决了,

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

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

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.

删除并重置字冠

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

注意:将版本号更改为错误消息中指示的版本号。

NVM不兼容NPM配置“prefix”选项:目前 set to "/usr/local"执行"npm config delete prefix"或"nvm use " ——delete-prefix v6.11.1——silent"


@gabfiocchi在Github -“你需要覆盖nvm前缀”