134

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

2
  • How did you edit the root PATH? Feb 12, 2011 at 7:41
  • 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
    Jun 20, 2011 at 14:20

15 Answers 15

333

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.

9
  • 1
    Lack of node-waf causes npm rebuild to error out. Is there a clean way to remedy this? Do I need to?
    – user730569
    Jun 6, 2012 at 2:34
  • 1
    This didn't work for me for a couple reasons. 1) I don't have sudo access. I'm on a shared host. 2) /usr/local doesn't exist. I made a directory ~/local, though. 3) After removing /usr and sudo from each of these lines of code and running them through my console, nothing had changed. Jul 5, 2013 at 22:15
  • 1
    That was helpfull. But can anybody expalin why and how it gets things working ? Dec 27, 2013 at 7:02
  • 2
    @Tarun: read up on the shell's PATH variable and how it is used to find which binary to execute for any given command. The ln command just links a second name to the same file. Dec 27, 2013 at 8:01
  • 1
    I'm using Amazon Linux. Is the line 'sudo ln -s /usr/local/lib/node /usr/lib/node' meant to be 'sudo ln -s /usr/local/lib/node_modules /usr/lib/node_modules'?
    – stevejay
    Jul 18, 2015 at 12:45
38

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!

2
  • 2
    great tip - I also did sudo ln -s /home/ec2-user/.nvm/versions/node/v8.11.3/bin/npm /usr/bin/npm as I was trying to do sudo npm install on my aws ec2 instance after following link Aug 14, 2018 at 6:10
  • You are a lifesaver and an absolute legend. +1
    – o_in25
    Jun 12 at 3:22
34

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
0
14

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
1
  • 2
    best solution, works and doesn't mess with the system like all ln solutions do.
    – SidOfc
    May 21, 2020 at 16:27
6

try the following:

export PATH=$PATH:/usr/local/bin
sudo node --version
1
  • 2
    Won't work for distributions which has secure_path set in sudoers file.
    – kenorb
    Dec 24, 2015 at 17:53
6

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

sudo `which node`
5

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

0
4

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

3

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

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

1

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

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

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

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

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.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.