127
votes

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

14
How did you edit the root PATH?Dennis Williamson
After much trying, I did this and it works: <pre> sudo su export PATH=$PATH:usr/local/node/ curl npmjs.org/install.sh | sh </pre>user806812

14 Answers

324
votes

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.

33
votes

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
29
votes

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!

13
votes

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
5
votes

try the following:

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

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

sudo `which node`
4
votes

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

4
votes

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

3
votes

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

1
votes

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

0
votes

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

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

0
votes

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.

-1
votes

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.