首页 > 解决方案 > 'sudo npm install -g npm' 失败

问题描述

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

操作系统版本是 Ubuntu,运行在 WSL 下Version 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。

标签: node.jsnpmwindows-subsystem-for-linux

解决方案


在过去的 2 天里,我遇到了同样的问题,但唉,我设法找到了解决方案。

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

选择nvm-setup.zip然后继续并按照安装过程进行操作。

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

然后,如果一切正常,请键入nvm install node

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


推荐阅读