I ran into a problem on my Pi yesterday; I could not update any npm installed packages. If I did try, it simply failed, quietly. Some searching led me to believe that it was because I had installed npm using sudo and possibly other packages as well (I’d lost some Bash history so I cannot be sure – a blog for another time).
I dug around a little and found this comment on StackOverflow;
“Recent versions of npm switch to user “nobody” as a security measure if it’s being run with sudo privileges.”
I could see exactly this within /usr/lib/node_modules
.
On the npm site itself, there is an item on this issue which suggests some modifications which I really did not look the like of, as
npm config get prefix returned just /usr
.
Another item I found suggested changing permissions in a similar manner to the npm docs but I felt it was rather over the top and changed too much. So I took a middle road and did the following;
1 2 |
sudo chown -R $(whoami) /usr/lib/node_modules |
This changed all the ‘nobody’ owned files and folders to owned by pi (which I was logged in as).
What I cannot remember (lost history again) is if I did it for the folders in my home folder. If I had found any files owned by ‘nobody’ I would have done so.
This has solved the problem and now I can use npm without sudo and update (both locally and globally) work fine.
HTH
by