【问题标题】:'sudo npm install -g npm' fails'sudo npm install -g npm' 失败
【发布时间】:2021-05-15 10:32:37
【问题描述】:

当我尝试通过 sudo npm install -g npm 更新 npm 时,Ubuntu 给了我错误

操作系统版本是Ubuntu,运行在WSLVersion 20H2 (OS build 19042.746)下,我在老版本的Win10上没有遇到过问题。

root@DESKTOP-D03G7FK:~# sudo npm update npm -g
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser
npm ERR! dest /usr/lib/node_modules/.staging/yargs-parser-b095ed45
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser' -> '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'
npm ERR!  [OperationalError: EACCES: permission denied, rename '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser' -> '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'] {
npm ERR!   cause: [Error: EACCES: permission denied, rename '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser' -> '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'] {
npm ERR!     errno: -13,
npm ERR!     code: 'EACCES',
npm ERR!     syscall: 'rename',
npm ERR!     path: '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser',
npm ERR!     dest: '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'
npm ERR!   },
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser',
npm ERR!   dest: '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-02-12T12_53_38_994Z-debug.log

问题还没解决,我切换到WSL2,因为WSL有Linux内核,所有问题都解决了,我认为是WSL的bug。

【问题讨论】:

  • 你会这样使用 sudo:sudo npm update npm -g 吗?
  • @Mohammad Yaser Ahmadi 当然,我使用root权限运行命令,我想也许我应该提供Windows版本,这可能是WSL的问题。
  • @mikemaccana 感谢您的链接,它似乎不在 WSL 中,我试过了,它不起作用。
  • @mikemaccana 既然你问我朋友,我可以继续使用旧版本的 npm,没问题。

标签: node.js npm windows-subsystem-for-linux


【解决方案1】:

过去 2 天我遇到了同样的问题,但很遗憾,我设法找到了解决方案。

首先,您需要安装nvm。您可以在此处找到最新版本并下载:nvm for windows

选择nvm-setup.zip,然后继续安装过程。

然后打开您的 Windows 终端,然后尝试输入 nvm -v 以查看它是否成功安装并具有最新版本。

如果一切顺利,请输入nvm install node

这会将您的节点更新到最新的可用版本。

【讨论】:

  • @forgaoqiang 可以复制粘贴错误日志吗?
【解决方案2】:

使用sudo /usr/bin/npm install -g npm 更新npm...这将使用使用Ubuntu 的包管理器安装的旧版本npm 进行引导。

当您安装了多个版本的 npm 或 nodejs 时,请始终在启动时使用完整路径,否则您可能会在运行时出错。

【讨论】:

  • 这在 WSL 环境中不起作用,无论如何谢谢
  • 在我的 WSL 安装上有效...不过我仍然使用 WSL 版本 1...
  • 不确定WSL,也许Linux的发行版也很重要,但是WSL仍然存在很多问题,尤其是NodeJS。
  • 最初的问题提到了 Ubuntu,所以我检查了 Ubuntu... 其他发行版可能会将 npm 安装到其他目​​录,但实际上没有太多选择。 WSL下基本上有两种问题......要么是新版本的npm有bug,要么是在当前帐户下首次运行npm时尝试使用“sudo”更改了npm目录的权限......有时它会更好改为使用“su”...
猜你喜欢
  • 1970-01-01
  • 2013-12-14
  • 2017-09-27
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 2017-02-14
相关资源
最近更新 更多