Running NPM command shows empty output for www-data user - node.js

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.

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

sudo: node: command not find on EC2 Ubuntu

When run sudo node --version on EC2 Ubuntu, error: sudo: node: command not find appears.
I have read similar questions and answers published in stackoverflow, but no one works in myside...
my node js is in /home/ubuntu/remoteserver/deviceServer.js, when I run sudo node deviceServer.js in the directory remoteserver, error: sudo: node: command not find appears. It is the same when I run outside the directory...
when I run whereis node, I got:
node: /usr/bin/node /usr/lib/node /home/ubuntu/.nvm/versions/node/v10.7.0/bin/node
when I run which npm, I got:
/home/ubuntu/.nvm/versions/node/v10.7.0/bin/npm
Could you please help?
It appears node in not in properties of root. But first of all, why do you need sudo to run node. You can directly do node deviceServer.js.
If you want compulsorily to use sudo. Execute below before running sudo node
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm

npm issue on EC2 instance

I download and compiled node and npm on EC2 instance. Everything seems right (the -v are shown on both), but when I try to npm install pm2 ask for me to run the command as Root (I am), yet when I sudo npm install says sudo: npm: command not found. Any ideas? Thanks.
This solved the issue.
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm

Accessing nodejs as non root

I have node 0.10.x installed via apt-get and also nvm/npm.
I used nvm to install node 0.12.7 and used this command
(n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local)
to copy the node binaries to /usr/local/ so that other users can use the version I chose using nvm. However getting the following error trying to do an npm install:
bash: /usr/local/bin/npm: /root/.nvm/versions/v0.12.7/bin/node: bad
interpreter: Permission denied
Any advice?

Dockerfile/npm: create a sudo user to run npm install

Creating a Dockerfile to install a node framework that we've created (per my earlier post here):
# Install dependencies and nodejs
RUN apt-get update
RUN apt-get install -y python-software-properties python g++ make
RUN add-apt-repository ppa:chris-lea/node.js
RUN apt-get update
RUN apt-get install -y nodejs
# Install git
RUN apt-get install -y git
# Bundle app source
ADD . /src
# Create a nonroot user, and switch to it
RUN /usr/sbin/useradd --create-home --home-dir /usr/local/nonroot --shell /bin/bash nonroot
RUN /usr/sbin/adduser nonroot sudo
RUN chown -R nonroot /usr/local/
RUN chown -R nonroot /usr/lib/
RUN chown -R nonroot /usr/bin/
RUN chown -R nonroot /src
USER nonroot
# Install app source
RUN cd /src; npm install
The problem is that npm expects to be run not as root -- is there a way to chain a series of sudo useradd commands to create a temp user that has sudo privileges that I can then switch to USER to run the npm install?
EDIT: updated the above, now getting this issue after successfuly creating a user and getting to the npm install line and choking:
Error: Attempt to unlock javascript-brunch#1.7.1, which hasn't been locked
at unlock (/usr/lib/node_modules/npm/lib/cache.js:1304:11)
at cb (/usr/lib/node_modules/npm/lib/cache.js:646:5)
at /usr/lib/node_modules/npm/lib/cache.js:655:20
at /usr/lib/node_modules/npm/lib/cache.js:1282:20
at afterMkdir (/usr/lib/node_modules/npm/lib/cache.js:1013:14)
at /usr/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
at Object.oncomplete (fs.js:107:15)
If you need help, you may report this *entire* log,
including the npm and node versions, at:
<http://github.com/npm/npm/issues>
The "Attempt to unlock" issue is often caused by not having the environment variable HOME set properly. npm needs this to be set to a directory that it can edit (it sets up and manages an .npm directory there).
You can specify environment variables in your docker run call with e. g. docker run -e "HOME=/home/docker".
To solve your "Attempt to unlock" issue, try cleaning the npm cache first by issuing
npm cache clean
After that, run
npm install
I came across a similar npm install error when I was trying to execute is as a non-root user in my Dockerfile. Svante's explanation of the issue is bang on, npm does some caching under the $HOME dir. Here's a simple Dockerfile that works with npm install:
FROM dockerfile/nodejs
# Assumes you have a package.json in the current dir
ADD . /src
# Create a nonroot user, and switch to it
RUN /usr/sbin/useradd --create-home --home-dir /usr/local/nonroot --shell /bin/bash nonroot
RUN chown -R nonroot /src
# Switch to our nonroot user
USER nonroot
# Set the HOME var, npm install gets angry if it can't write to the HOME dir,
# which will be /root at this point
ENV HOME /usr/local/nonroot
# Install app source
WORKDIR /src
RUN npm install

On EC2: sudo node command not found, but node without sudo is ok

I have just installed nodejs on a new EC2 micro instance.
I installed it normally, ./configure -> make -> sudo make install.
Problem: When I run "node" under ec2-user, it runs perfectly. When I run "sudo node", it fails.
I found out that node is in:
[ec2-user#XXXX ~]$ whereis node
node: /usr/local/bin/node /usr/local/lib/node
and the current path is
[ec2-user#XXXX ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/opt/aws/bin:/home/ec2-user/bin
but, the sudo path is
[root#ip-10-112-222-32 ~]# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin
then I tried to edit the root PATH to include the paths to node, so "node" runs when I'm logged in as root - but it still won't work when I log in as ec2-user and run "sudo node".
I need this to install npm properfly. Any idea on how to include the node path while running "sudo node"?
Yes, it is a bit annoying but you can fix it with some links:
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
There might be more but that is all I have run across so far. Lack of node-waf will cause some npm installs to fail with a rather cryptic error message.
I added /usr/local/bin to secure_path in /etc/sudoers
$ sudo visudo
Then change this line:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
To:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
it happens because the node executable is not found in /usr/bin. So follow the steps:
find node:
whereis node
in my case: node: /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node
make a symbolic link for node:
sudo ln -s /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node /usr/bin/node
It's done!
Why not use the absolute path to node? If you planning to use an upstart script it is going to need an absolute path anyways.
sudo /usr/local/bin/node server.js
try the following:
export PATH=$PATH:/usr/local/bin
sudo node --version
You could pass full path to node executable from parent (non-sudo shell) using which command.
sudo `which node`
For me, it worked to just change ownership of node folder from root to ec2-user (logged in as ec2-user).
(Note: I created my node folder in /var/lib/)
sudo chown -R ec2-user /var/lib/node/
Then
npm install mongojs
should work fine (provided you have installed npm ok of course!)
How about using "sudo $(which node)" instead of "sudo node" ?
Here's an approach that doesn't use symlinks, or require root:
$ git clone https://github.com/joyent/node.git
$ cd node
$ mkdir ~/opt
$ export PREFIX=~/opt; ./configure
$ make
$ make install
$ echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc
Then I did:
$ git clone https://github.com/isaacs/npm.git
$ cd npm
$ make install
The benefits of not running node as root are discussed here:
http://increaseyourgeek.wordpress.com/2010/08/18/install-node-js-without-using-sudo/
Its inline with:
https://github.com/joyent/node/wiki/Installation
In my case, Node was installed without sudo prefix. So node was unavailable for the superuser that why it is not working sudo node server
Enter as root with
sudo su
and then do standard steps
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
node -e "console.log('Running Node.js ' + process.version)"
This is what I did to solve a similar issue. Note: I had installed node via snap.
Step 1: Install node via snap
sudo snap install node --classic
Step 2: Find where node has been installed
whereis node
In my case
/snap/bin/node.npm
/snap/bin/node.npx
/snap/bin/node.yarn
/snap/bin/node
/snap/bin/node.yarnpkg
Step 3: Create symbolic links to node, npm, npx and yarn
sudo ln -s /snap/bin/yarn /usr/bin/yarn
sudo ln -s /snap/bin/node /usr/bin/node
sudo ln -s /snap/bin/npm /usr/bin/npm
Finally node is accessible for all users, even sudo group
sudo node
I tried everything mentioned in the above answers but nothing worked. Probably because of my lack of understanding of concepts related to sym links. I fixed the issue by not using nvm.
I just created a new ec2 instance and didn't install nvm. Instead I installed node and npm like so:
sudo apt update
sudo apt install nodejs npm
And this did it for me.
If you have installed node environment already, paste these command in you r instance and it'll link your node to your user directory. And you will be able to use node with sudo.
sudo ln -s "$(which node)" "/usr/local/bin/node"
sudo ln -s "$(which npm)" "/usr/local/bin/npm"
I don't know if this is the right way, but this is what i did...
sudo su
export PATH=$PATH:/home/ec2-user/local/node/bin
curl http://npmjs.org/install.sh | sh
chown -R ec2-user /home/ec2-user/local/node
exit
This installed npm, and I can now install any packages I want.

Resources