为什么在安装最新版本的 npm 时出现错误?

Why am I getting an error when installing the latest version of npm?

我当前的 npm 版本是 3.5.2。我想更新 npm 和 sudo npm install npm @latest -g 我正在输入这个命令。我尝试删除 package.json 但我在下面收到此错误。

我今天遇到了类似的问题。没有任何效果,我发现了很多类似问题的错误报告。解决方案是 use a version manager like nvm.

(可选)如果您是 运行 Ubuntu,您可以像这样删除当前安装的系统范围版本的 nodejsnpm

sudo apt remove nodejs npm

以下是我输入的用于在我的用户目录中安装新版本的确切命令。结果,所有权限问题都消失了。这些命令可能适用于所有操作系统,只需稍作改动:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
source ~/.bashrc  # this line might change
nvm install v15.12.0
npm install -g npm@7.7.5

如果您有 Mac,则改为 source ~/.bash_profile。 ...除非你是 运行 zsh,在这种情况下你需要 source ~/.zshrc。如果一切都失败了,source ~/.profile.

For more info