Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

npm update -g fails and causes /usr/local/lib/node_modules to be deleted #8165

Closed
ghost opened this issue May 2, 2015 · 35 comments
Closed

Comments

@ghost
Copy link

ghost commented May 2, 2015

I have no idea what's going on here, but it's way messed up.

I install node/npm via the official website. Of course it installs as admin, which is problematic when you try to run sudo npm update -g. I've always gotten permission errors, so I think the general consensus is to do sudo chown -R myusername:staff /usr/local/lib/node_modules.

After doing that, I run npm update -g. I get the following:

npm ERR! Darwin 14.3.0
npm ERR! argv "node" "/usr/local/bin/npm" "update" "-g"
npm ERR! node v0.12.2
npm ERR! npm  v2.7.4
npm ERR! path /usr/local/share/man/man7/semver.7
npm ERR! code EACCES
npm ERR! errno -13

npm ERR! Error: EACCES, unlink '/usr/local/share/man/man7/semver.7'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES, unlink '/usr/local/share/man/man7/semver.7']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/share/man/man7/semver.7' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /usr/local/lib/npm-debug.log

And then boom, the entire /usr/local/lib/node_modules folder vanishes. So I have to remove semver.7 man page, reinstall the Node.js package again, and start over.

Is there a clean way to uninstall Node.js for Mac?

@ghost
Copy link
Author

ghost commented May 2, 2015

If I run sudo npm update -g after a fresh install of Node.js, and then change ownership of /usr/local/lib/node_modules to me, and then run npm update -g, it works ok.

@seanstrom
Copy link

@CaptSaltyJack
Quick question, if you're on Mac OSX are you currently using a package manager like Brew? If not I would recommend using Brew in order to install Node, which would also come with npm. At that point you could update npm to the latest version with the permissions already handled for you. It's pretty painless this way.

@smikes
Copy link
Contributor

smikes commented May 2, 2015

Thanks for reporting this!

npm update -g was broken in versions prior to npm@2.6.1, and would blow up in exactly that way (deleting itself), or even worse. The only reason it was left broken for so long is that there were even worse race conditions and git repository errors that needed to be dealt with.

Fortunately, quite a lot of this deferred maintenance has been finished recently. I'm sorry that it still bit you -- or possibly, this is even a new error that bit you.

For future reference, the preferred way to do an update of npm (without updating node) is:

npm install -g npm@latest

It's important to have a recent npm if you're going to be running node@>=0.11 -- which include iojs, too -- because there is a bug in the http-keepalive code that is triggered by a bug in npm<2.8.2

Thanks!

@silverwind
Copy link

This issue exists in the 2.9.0 in the case of npm install -g npm when the symlink at /usr/local/bin/npm is user-writeable but /usr/local/lib/node_modules/npm/ is not. I think the update mechanism removes the symlink to too soon.

@smikes
Copy link
Contributor

smikes commented May 2, 2015

Thanks for following up; it sounds like we can do more to avoid the problem on updates. Before 2.7 'npm update' was totally broken; it would try to update npm's own dependencies while npm was running.

Now it is only sometimes broken...

@silverwind
Copy link

Yeah, it happens to me when I forgot to sudowhen updating npm. I'm not sure my permission setup in /usr is standard though :)

@smikes
Copy link
Contributor

smikes commented May 2, 2015

@silverwind if you sudo chmod 755 /usr/local/bin/npm , will that prevent sudo-less npm install -g npm@latest from blowing up?

@silverwind
Copy link

@smikes unfortunately not, but my permissions are a real messy:

$ ls -la $(which npm)
lrwxr-xr-x  1 root  admin  38 May  3 00:28 /usr/local/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js
$ ls -la /usr/local/lib/node_modules/npm/bin/npm-cli.js
-rwxrwxr-x  1 macports  staff  1885 Apr 30 00:00 /usr/local/lib/node_modules/npm/bin/npm-cli.js

The actual error I'm getting on npm update -g is

EACCES: permission denied, unlink '/usr/local/lib/node_modules/npm/.eslintrc'

which is

-rw-rw-r--  1 macports  staff  435 Apr 30 00:00 /usr/local/lib/node_modules/npm/.eslintrc

As far as I can tell the only issue is the missing symlink after a failed update, running /usr/local/lib/node_modules/npm/bin/npm-cli.js directly still works after it blew up.

@smikes
Copy link
Contributor

smikes commented May 2, 2015

@captSaltyJack can you check whether just the link is missing or the whole node_modules/lib directory?

On Sat, May 2, 2015 at 4:39 PM, silverwind notifications@github.com
wrote:

@smikes unfortunately not, but my permissions are a real messy:

$ ls -la $(which npm)
lrwxr-xr-x  1 root  admin  38 May  3 00:28 /usr/local/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js
$ ls -la /usr/local/lib/node_modules/npm/bin/npm-cli.js
-rwxrwxr-x  1 macports  staff  1885 Apr 30 00:00 /usr/local/lib/node_modules/npm/bin/npm-cli.js

The actual error I'm getting on npm update -g is

EACCES: permission denied, unlink '/usr/local/lib/node_modules/npm/.eslintrc'

which is

-rw-rw-r--  1 macports  staff  435 Apr 30 00:00 /usr/local/lib/node_modules/npm/.eslintrc

As far as I can tell the only issue is the missing symlink after a failed update, running /usr/local/lib/node_modules/npm/bin/npm-cli.js directly still works after it blew up.

Reply to this email directly or view it on GitHub:
#8165 (comment)

@ghost
Copy link
Author

ghost commented May 2, 2015

Hey guys, sorry I can't be of much help. My install is working (and I have to keep it that way so I can get some stuff done). But no, the entire /usr/local/lib/node_modules folder was missing.

@silverwind
Copy link

Probably a different issue than mine. As for mine, a fix would probably to check write permission in both the global module folder as well as the symlink location before unlinking anything.

@othiym23
Copy link
Contributor

othiym23 commented May 7, 2015

This is the first time I've heard of npm update -g actually removing the entire /usr/local/lib/node_modules directory, so I really need a repro case before I can do anything with this. The other responders to this issue describe the situation we normally see – user has write access to /usr/local/bin, but not /usr/local/lib/node_modules, so the symbolic link gets removed, even though the npm install is still in /usr/local/lib/node_modules/npm. If you can show me how to reproduce this, I'll get right on it, because that is indeed a serious bug.

@othiym23 othiym23 changed the title MAJOR Mac OS issue - npm self-deletes entirely npm update -g fails and causes /usr/local/lib/node_modules to be deleted May 7, 2015
@silverwind
Copy link

npm is installed along with io.js through the official io.js 2.0 installer.

The content of /usr/local/lib/node_modules/npm after installing is:

drwxrwxr-x 26 root       wheel    884 May  7 06:54 .
drwxr-xr-x 44 silverwind staff   1496 May  7 06:55 ..
drwxrwxr-x  7 root       wheel    238 May  7 06:54 bin
drwxrwxr-x  6 root       wheel    204 May  4 23:42 doc
drwxrwxr-x  9 root       wheel    306 May  7 06:54 html
drwxrwxr-x 59 root       wheel   2006 May  7 06:54 lib
drwxrwxr-x  6 root       wheel    204 May  4 23:42 man
drwxrwxr-x 69 root       wheel   2346 May  4 23:43 node_modules
drwxrwxr-x 10 root       wheel    340 May  7 06:54 scripts
drwxrwxr-x 10 macports   staff    340 May  4 23:38 test
-rw-rw-r--  1 macports   staff    435 May  4 23:38 .eslintrc
-rw-rw-r--  1 macports   staff   1745 May  4 23:38 .mailmap
-rw-rw-r--  1 macports   staff    490 May  4 23:38 .npmignore
-rw-rw-r--  1 macports   staff     44 May  4 23:38 .npmrc
-rw-rw-r--  1 macports   staff    307 May  4 23:38 .travis.yml
-rw-rw-r--  1 macports   staff   9972 May  4 23:38 AUTHORS
-rw-rw-r--  1 macports   staff 162328 May  4 23:38 CHANGELOG.md
-rw-rw-r--  1 macports   staff    503 May  4 23:38 CONTRIBUTING.md
-rw-rw-r--  1 macports   staff  11319 May  4 23:38 LICENSE
-rw-rw-r--  1 macports   staff   7376 May  4 23:38 Makefile
-rw-rw-r--  1 macports   staff   7203 May  4 23:38 README.md
-rwxrwxr-x  1 macports   staff     48 May  4 23:38 cli.js
-rwxrwxr-x  1 macports   staff    521 May  4 23:38 configure
-rw-rw-r--  1 macports   staff    156 May  4 23:38 make.bat
-rw-rw-r--  1 macports   staff   4524 May  4 23:38 package.json
-rw-rw-r--  1 macports   staff    646 May  4 23:38 wercker.yml

Note that I have no idea where that macports user comes from, as I've long switched to brew, but I assume it doesn't matter in this case. I'm unable to delete any file in this folder without elevating.

/usr/local/bin/npm is:

lrwxr-xr-x 1 root admin 38 May  7 07:00 /usr/local/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js

Based on these permissions and owner, I'd assume only root can delete this symlink, but I've manually tested it and was still able to. Maybe I'm missing something here.

After installing, I run npm install -g npm@latest, giving:

npm ERR! Darwin 14.4.0
npm ERR! argv "/usr/local/bin/iojs" "/usr/local/bin/npm" "install" "-g" "npm@latest"
npm ERR! node v2.0.0
npm ERR! npm  v2.9.0
npm ERR! path /usr/local/lib/node_modules/npm/.eslintrc
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall unlink

npm ERR! Error: EACCES: permission denied, unlink '/usr/local/lib/node_modules/npm/.eslintrc'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, unlink '/usr/local/lib/node_modules/npm/.eslintrc']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'unlink',
npm ERR!   path: '/usr/local/lib/node_modules/npm/.eslintrc' }

And the symlink is gone. Same happens when I try to update any globally installed module. Hope that helps.

@othiym23
Copy link
Contributor

othiym23 commented May 7, 2015

@silverwind That's the expected failure mode (which, by the by, is addressed in multi-stage / npm@3 by waiting to remove the link until we already know that we have privileges to to overwrite directories in the existing global node_modules).

Based on these permissions and owner, I'd assume only root can delete this symlink, but I've manually tested it and was still able to. Maybe I'm missing something here.

The important privileges are those on /usr/local/bin, not the symlink itself. The privileges there only affect reading and writing its contents, not removing the file, where the removal is an operation on the directory index, rather than the symlink itself.

@silverwind
Copy link

Indeed, /usr/local/bin is writeable:

drwxr-xr-x 941 silverwind admin 31994 May  7 07:12 bin

Also, thanks for the unix permission lesson 😉

@silverwind
Copy link

By the way, my issue was only about the symlink deletion, not the whole directory. Sorry for hijacking this issue with that.

@smikes
Copy link
Contributor

smikes commented May 7, 2015

Also, thanks for the unix permission lesson 😉

Me too! I was incorrectly thinking about permissions on the link (above).

It sounds like @CaptSaltyJack has been able to work around this - can this now be closed, or should we keep it open?

@silverwind
Copy link

If I understand @othiym23 correctly, the symlink thing is fixed already in npm@3? If that's correct, I'd vote to closer this too.

@blacksab
Copy link

blacksab commented Feb 2, 2016

I had the same issue and resolved it with this:
https://docs.npmjs.com/getting-started/fixing-npm-permissions

@shentongzoe
Copy link

@jozhi leifeng 666

@bneiluj
Copy link

bneiluj commented Aug 9, 2016

Do not use sudo as you may mess eveything up ! 👎
Just set correctly the ownership: sudo chown -R $(whoami) /usr/local/lib/node_modules/

@linardsb
Copy link

linardsb commented Oct 12, 2016

I might have a similar issue, I tried to install the npm install pug could not because of the permisions, got this: checkPermissions Missing write access to /usr/local/lib/node_modules however im logged in as as root Linards:~ Berzins$, as I tried to enable the root user:

`Linards:~ Berzins$ dsenableroot
username = Berzins
user password:
root password:
verify root password:

dsenableroot:: ***Successfully enabled root user.`

Any help appreciated.

@d3v-studio
Copy link

Run these commands in a terminal window (note - DON'T replace the $USER part... thats a Linux command to get your user!):

sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
sudo chown -R $USER /usr/local/lib/node_modules

@VGerris
Copy link

VGerris commented Nov 30, 2016

Although that works, I am not a fan of changing system directories own by root to be wrtiable by the user.
A better and cleaner way in my opinion is as the nodejs page suggest under option 2 here:
https://docs.npmjs.com/getting-started/fixing-npm-permissions

to repeat:

Make a directory for global installations:

mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
source ~/.profile

That works with the ubuntu default packages installed by:
sudo apt-get install npm nodejs
and keeps permissions clean.

@codengains
Copy link

codengains commented Jan 26, 2017

I don't remember where I got this command but it worked for me so I saved it in google drive.

echo prefix = ~/.node >> ~/.npmrc

@npm-robot
Copy link

We're closing this issue as it has gone thirty days without activity. In our experience if an issue has gone thirty days without any activity then it's unlikely to be addressed. In the case of bug reports, often the underlying issue will be addressed but finding related issues is quite difficult and often incomplete.

If this was a bug report and it is still relevant then we encourage you to open it again as a new issue. If this was a feature request then you should feel free to open it again, or even better open a PR.

For more information about our new issue aging policies and why we've instituted them please see our blog post.

@Extarys
Copy link

Extarys commented Sep 17, 2017

Had this issue 5 min ago. still trying to figure out how to fix this.

@mltsy
Copy link

mltsy commented Sep 26, 2017

@VGerris' solution worked for me: #8165 (comment)

Thanks! 😉

@csubbum
Copy link

csubbum commented Nov 19, 2017

super user do helped

@Jolley71717
Copy link

I tried creating a separate drive as well as reinstalling node and npm, but I've had no luck.

@sealrockswell

This comment has been minimized.

@VGerris
Copy link

VGerris commented Jan 29, 2018

Or read back and see how to do it in a more controlled way.

@yyhinbeijing
Copy link

you can try:
sudo chown -R $USER /usr/local

@korayoksuztepe
Copy link

sudo chown -R $USER:$(id -gn $USER) /usr/local/lib/node_modules
fixed it for me.

@mltsy
Copy link

mltsy commented Mar 16, 2018

Just want to point out again, for posterity, that this solution is recommended on the nodejs page and doesn't require resetting any permissions: #8165 (comment)

@npm npm locked as resolved and limited conversation to collaborators May 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests