4

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.

  • 1
    do you use sudo like this : `sudo npm update npm -g` ? – Mohammad Yaser Ahmadi Feb 11 '21 at 16:45
  • Does this answer your question? [On npm install: Unhandled rejection Error: EACCES: permission denied](https://stackoverflow.com/questions/50639690/on-npm-install-unhandled-rejection-error-eacces-permission-denied) – mikemaccana Feb 11 '21 at 16:49
  • 1
    @Mohammad Yaser Ahmadi Of course, I use the root permission to run the command, I think maybe I should provide the Windows Version, it could be the problem with the WSL. – forgaoqiang Feb 11 '21 at 18:21
  • @mikemaccana Thanks for the link, it seems not in WSL, and I tried that, it doesn't work. – forgaoqiang Feb 11 '21 at 18:25
  • @forgaoqiang can you paste what happened when you tried with `sudo npm update npm -g` into your question above? – mikemaccana Feb 11 '21 at 18:41
  • 1
    @mikemaccana Since you asked my friend, I can just keep using the older version of npm, no problem with that. – forgaoqiang Feb 12 '21 at 12:55
  • @forgaoqiang It sounds like you do indeed have a problem. I would suggest making the question 'npm complains about 'permission denied' errors when running via sudo' – mikemaccana Feb 12 '21 at 14:14

2 Answers2

1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
gjove
  • 26
  • 2
0

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.

  • That won't work in WSL environment, thanks anyway – forgaoqiang Jun 19 '21 at 15:30
  • 1
    Does work on my WSL installation... I still use version 1 of WSL though... – Mika Lindqvist Jun 20 '21 at 17:11
  • Not sure about the WSL, maybe the distribution of Linux matters too, but there are still many problems in WSL, especially NodeJS. – forgaoqiang Jun 23 '21 at 07:28
  • The original question mentioned Ubuntu, so I checked with Ubuntu... Other distributions might install npm to other directory, but there isn't really many choices. There is basically two kinds of problems under WSL... Either the newer version of npm is buggy or the permissions for npm directories were changed by trying to use "sudo" on first run of npm under the current account... Sometimes it's better to use "su" instead... – Mika Lindqvist Jun 24 '21 at 11:08