'sudo npm install -g npm' fails

Viewed 1629

When I try to update npm by sudo npm install -g npm, Ubuntu gives me the error

The OS version is Ubuntu, running under WSL Version 20H2 (OS build 19042.746), I haven't encountered the problem in an older version of 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

The problem hasn't been solved, I switched to WSL2, since the WSL has the Linux kernel and all the problems were solved, I think it's the bug of WSL.

2 Answers

I had this same problem for the past 2 days but alas, I managed to find a solution.

First, you will want to install nvm. You can find the latest version and download here: nvm for windows

Choose the nvm-setup.zip then proceed and follow the installation process.

After that open your windows terminal then try type nvm -v to see if it successfully installed and have the latest version.

Then if all is good, type nvm install node.

This will update your node to the latest available version.

Use sudo /usr/bin/npm install -g npm to update npm... This will bootstrap using older version of npm that was installed using Ubuntu's package manager.

When you have multiple versions of npm or nodejs installed, always use full path when starting as otherwise you might get errors while running.

Related