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

node.jsBashAmazon Ec2Sudo

node.js Problem Overview


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"?

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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

Solution 3 - node.js

it happens because the node executable is not found in /usr/bin. So follow the steps:

  1. find node:

whereis node

in my case: node: /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node

  1. 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!

Solution 4 - node.js

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

Solution 5 - node.js

try the following:

export PATH=$PATH:/usr/local/bin
sudo node --version

Solution 6 - node.js

You could pass full path to node executable from parent (non-sudo shell) using which command.

sudo `which node`

Solution 7 - node.js

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!)

Solution 8 - node.js

How about using "sudo $(which node)" instead of "sudo node" ?

Solution 9 - node.js

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

Solution 10 - node.js

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

Solution 11 - node.js

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)"

Solution 12 - node.js

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

Installing node via snap for all users, ubuntu, debian

Solution 13 - node.js

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.

Solution 14 - node.js

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfoobarView Question on Stackoverflow
Solution 1 - node.jsMichael DillonView Answer on Stackoverflow
Solution 2 - node.jsJack FrostView Answer on Stackoverflow
Solution 3 - node.jsJeff PalView Answer on Stackoverflow
Solution 4 - node.jsShripad KrishnaView Answer on Stackoverflow
Solution 5 - node.jsAmroView Answer on Stackoverflow
Solution 6 - node.jsPavel ZubkouView Answer on Stackoverflow
Solution 7 - node.jsuser1839216View Answer on Stackoverflow
Solution 8 - node.jsWill VoelckerView Answer on Stackoverflow
Solution 9 - node.jsJack MurphyView Answer on Stackoverflow
Solution 10 - node.jsshubhamView Answer on Stackoverflow
Solution 11 - node.jsBaimyrza ShamyrView Answer on Stackoverflow
Solution 12 - node.jsslimgera00View Answer on Stackoverflow
Solution 13 - node.jsFarhan AliView Answer on Stackoverflow
Solution 14 - node.jsbdavisView Answer on Stackoverflow