Solution 1:

My npm version is 5.0.3 and I have the same problem when I use any npm command:

npm error message

The command is working fine but this code keeps showing up for no apparent reason. I've updated from 5.0.3 to 5.2.0 with these commands:

sudo chown -R $USER:$(id -gn $USER) /home/sagar/.config

npm install -g npm or npm i -g npm.

Now npm is working fine.

Solution 2:

Exact same issue for me, but doesn't seem related to the version (I'm using npm 5.5.1). Fixing the rights on ~/.config made the trick for me:

sudo chown -R $USER:$(id -gn $USER) ~/.config

I guess this is related to some owner issue with ~/.config/configstore/update-notifier-npm.json.

Solution 3:

Please, do not use the provided chown command in the error message. The path ~/.config is used by many applications and is a standard path for storing config information. It is not recommended to change the group rights there. Better only change the required path like:

sudo chown -R $USER ~/.config/configstore

Special case: Docker

A common case to get this error is using something like Docker (especially by mounting your own home). Consider setting the environment variables like NPM_CONFIG_CACHE and XDG_CONFIG_HOME. The first one sets your cache path and the last one is setting your configstore path. I would not use the .config folder in the home directory to avoid problems with file ownership. But using cache helps services like NPM to save bandwith. Try to avoid excessive downloads on every build and use something like this (but check that the target like /tmp has sufficient disk space):

test -d /tmp/$USER/cache || mkdir -p /tmp/$USER/cache
docker run \
  -v /tmp/$USER:/tmp/$USER \
  -e "NPM_CONFIG_CACHE=/tmp/$USER/cache/npm" \
  -e "XDG_CONFIG_HOME=/tmp/$USER/cache/" \
  ...

Solution 4:

I had change the owner of

~/configstore

and

~/configstore/update-notifier-npm.json

to $USER:$USER .

My guess is that running npm with sudo created that file. On my system at least we find this:

$ env | grep  -e HOME= -e "^USER="
USER=craig
HOME=/home/craig

$ sudo env | grep  -e HOME= -e "^USER="
HOME=/home/craig
USER=root

So if sudo npm is coded to create ~/configstore/update-notifier-npm.json it's going to do it the ordinary user's home directory. That creates a problem when later running npm without sudo which also wants to read and maybe write ~/configstore/update-notifier-npm.json. For some reason the permissions are 600 on both.

Be cautious when chown and chmod -ing everything under ~/.config, there might be something sensitive there.