Upgrading npm to latest fails with permission error

What I Wanted to Do

I want to upgrade npm on my system from v6.9.0 to latest (v6.10.0).

What Happened Instead

I received an “EACCESS -13 rename” permission denied error.

Reproduction Steps

  • Upgrade npm using npm i -g npm, npm i -g npm@6.10.0 or nvm install-latest-npm
  • Receive the following ERROR:
    (See attached log)
npm ERR! path /home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall rename
npm ERR! Error: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'
Full Error
npm ERR! path /home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall rename
npm ERR! Error: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'
npm ERR!  [OperationalError: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'] {
npm ERR!   cause: [Error: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'] {
npm ERR!     errno: -13,[2019-07-04T12_16_30_832Z-debug.log|attachment](upload://vZW7ZqZhF5RZFEnqZLIDQk7J5k7.log) (118.0 KB) 
npm ERR!     code: 'EACCES',
npm ERR!     syscall: 'rename',
npm ERR!     path: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs',
npm ERR!     dest: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'
npm ERR!   },
npm ERR!   stack: "Error: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs',
npm ERR!   dest: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'
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 (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2019-07-04T12_16_30_832Z-debug.log

Details

I’m running Ubuntu on WSL on Windows 10. All systems have been upgraded to the latest versions. I didn’t have this issue on my system until recently.

I first encountered the error without nvm on pure system npm.
Switching to nvm did not solve my issue.

There should be no permissions issue:

drwxrwxrwx 1 user user 512 Jul  4 14:01 ~/.nvm
drwxr-xr-x 1 user user 512 Jul  4 14:16 ~/.nvm/versions/node/v12.6.0/node_modules

I’ve tried with and without unsafe-perm = true and user = 0 without success.

Downgrading to older versions (npm i -g npm@6.8.0) fails with the same error.

Platform Info

$ npm --versions
{
  npm: '6.9.0',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '1.1.4',
  modules: '72',
  napi: '4',
  nghttp2: '1.38.0',
  node: '12.6.0',
  openssl: '1.1.1c',
  tz: '2019a',
  unicode: '12.1',
  uv: '1.30.1',
  v8: '7.5.288.22-node.14',
  zlib: '1.2.11'
}
$ node -p process.platform
linux

The short version is run this to fix the ownership, then try your install again:

sudo chown -R $(whoami) ~/.nvm

(I am expecting there is a file or folder with root ownership left over from a global install using sudo at some point.)

That shouldn’t be possible though. I’ve purged all npm and nvm installations and reinstalled fresh without using sudo.

I’ve tried running your command but it did not change the behavior I described in my bug report.

(I am unsure if you are currently using nvm.)

The error message you included had .nvm in the path. Is that still the case?

How have you installed without using sudo? Are you using nvm?

What do these show:

npm prefix -g
command -v npm

Yes I’m using nvm. I installed it fresh using the curl command as described in the docs without sudo. The prefix is manged by nvm.

What are the permissions on the folder mentioned in the error message?

ls -ld /home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs

The .staging directory does not exist. It’s likely an ethereal directory used during the installation process.

$ ls -ld /home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs
ls: cannot access '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs': No such file or directory
~/.nvm/versions/node/v12.6.0/lib/node_modules$ ls -alh
total 0
drwxr-xr-x 1 user user 512 Jul  5 03:04 .
drwxr-xr-x 1 user user 512 Jul  3 13:47 ..
drwxr-xr-x 1 user user 512 Jul  3 13:47 npm
~/.nvm/versions/node/v12.6.0/lib$ ls -alh
total 0
drwxr-xr-x 1 user user 512 Jul  3 13:47 .
drwxrwxrwx 1 user user 512 Jul  4 14:01 ..
drwxr-xr-x 1 user user 512 Jul  5 03:04 node_modules

There doesn’t appear to be any issue with the unix permissions on these folders.

(Drat. I am out of questions and suggestions!)

I have exactly the same problem…
If someone can help :slight_smile:

Ok. I fix it with this doc: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

Hey, this fix requires you to change the prefix and that isn’t compatible with nvm. It’s not really a solution to this issue then.

Are there any known solutions that would work with nvm?

I have seen issues with past versions where the .staging directory tries to move before it’s ready. It’s weird that you’re getting EACCES errors when all the ownership seems fine.

Do you have a virus scanner or anything else that might be locking the directory?

The “custom prefix in $HOME” solution really is what nvm does by default, so yeah, that’s not it.

Out of curiosity:

  • What happens when you run it with sudo?
  • Are there root-owned files in your cache? find $(npm get cache) -uid 0

Another possible avenue to explore:

git clone https://github.com/npm/cli npm
cd ./npm
git checkout -t origin/release-next # or origin/latest for npm@latest
make install

That’ll build all the docs and then pack it into a local tarball and install that globally. At least then you’re using the latest npm to install it, so if it’s an issue that’s been fixed, you won’t see the problem.

Thank you for your response!

I ran find $(npm get cache) -uid 0 but it finished without a result.

Using sudo won’t work as nvm only runs as the global user. It can’t find the command npm.

For building npm locally – won’t that interfere with nvm? I’ve started the build with the commands you’ve proposed.

It shouldn’t interfere any more than just installing npm globally would.

When you run make install, npm infers the global prefix from the location of the node binary running it, so it ought to drop it in the same place that npm i -g npm would. (In fact, what it’s actually doing is just running node . pack and then node . install <tarball generated> to install the tarball, instead of using the one downloaded from the registry.)

The relevant variable in make install is that you’d be installing npm using the latest npm, rather than using whatever version nvm gave you (which might be the source of the bug). If make install has the same issue, than it’s a current bug.

make install is presenting the same issue:

...
npm notice ssri
npm notice opener
npm notice path-is-inside
npm notice sorted-object
npm notice === Tarball Details ===
npm notice name:          npm
npm notice version:       6.10.1
npm notice filename:      npm-6.10.1.tgz
npm notice package size:  4.7 MB
npm notice unpacked size: 18.7 MB
npm notice shasum:        be7ced3483f4e720a285a026f9c2bdde06d685d6
npm notice integrity:     sha512-a2mvSHffQjEXS[...]q11T/xOUcS2vw==
npm notice bundled deps:  122
npm notice bundled files: 3629
npm notice own files:     475
npm notice total files:   4104
npm notice
node bin/npm-cli.js install -g -f  npm-6.10.1.tgz
npm WARN using --force I sure hope you know what you are doing.
npm ERR! path /home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall rename
npm ERR! Error: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'
npm ERR!  [OperationalError: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'] {
npm ERR!   cause: [Error: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'] {
npm ERR!     errno: -13,
npm ERR!     code: 'EACCES',
npm ERR!     syscall: 'rename',
npm ERR!     path: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs',
npm ERR!     dest: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'
npm ERR!   },
npm ERR!   stack: "Error: EACCES: permission denied, rename '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs' -> '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/npm-60bf52cc/node_modules/yargs',
npm ERR!   dest: '/home/user/.nvm/versions/node/v12.6.0/lib/node_modules/.staging/yargs-81a9147b'
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 (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2019-07-17T17_54_57_138Z-debug.log
Makefile:60: recipe for target 'install' failed
make: *** [install] Error 243

Oh, another thing that occurs to me: you’re running under WSL. Is your home directory mounted from the Windows environment? That might be making things weird.

If so, does it work if you do this?

# first, remove the NVM bits from ~/.profile, ~/.bash_profile, and/or ~/.bashrc
unset NVM_DIR
unset NVM_CD_FLAGS
unset NVM_BIN
unset NVM_RC_VERSION

sudo mkdir -p /newhome/.config
sudo chown -R $USER:$GROUP /newhome
export XDG_CONFIG_HOME=/newhome/.config
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
which npm # verify that it's finding it in /newhome/.config/versions/node/...
npm i -g npm@next

The home directory is not mounted but just acts as a regular unix directory in /home/user.

Regardless I installed nvm under /newhome/.config.

$ which npm
/newhome/.config/versions/node/v12.6.0/bin/npm

And the error persists.

Error: EACCES: permission denied, rename '/newhome/.config/versions/node/v12.6.0/lib/node_modules/.staging/npm-3c4f3ca2/node_modules/yargs' -> '/newhome/.config/versions/node/v12.6.0/lib/node_modules/.staging/yargs-c80cc2b0'

I am hoping to do an upgrade to WSL2 soon but I’m waiting for the current limitations in networking to be resolved.