Running NPM command shows empty output for www-data user

Viewed 120

Node v16.16.0 is installed by running the next terminal commands:

curl -s https://deb.nodesource.com/setup_16.x | sudo bash;
sudo apt install nodejs -y.

After installation, running a command:

sudo node -v

outputs:

v16.16.0

Running the same one as a www-data user:

sudo -u www-data node -v

outputs as the previous one as well:

v16.16.0

But the problem with npm command. As long as the command runs within sudo output has version

sudo npm -v

And the output is:

8.11.0

However, running as www-data outputs empty line.

sudo -u www-data npm -v

The same problem is for any npm command.

The question is why is this happening and how to make npm commands work for www-data user.

1 Answers

Check if your home folder for www-data is writable. You have to create folder .npm in it (and grant access for www-data). In my case I had to do

mkdir /var/www/html/.npm/ && chown www-data:www-data /var/www/html/.npm/

I am not a skilled linux guy but I believe it could be also done more elegantly using ~/.npm

Related