126

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

devnull
  • 103,635
  • 29
  • 207
  • 208
foobar
  • 9,276
  • 14
  • 46
  • 63

13 Answers13

322

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.

Michael Dillon
  • 30,332
  • 5
  • 65
  • 99
  • I am just curious, does this linking has to be done only in Amazon AMI system? Does Amazon AMI system separate root path from user path? – user482594 Feb 22 '12 at 20:45
  • Lack of `node-waf` causes `npm rebuild` to error out. Is there a clean way to remedy this? Do I need to? – user730569 Jun 06 '12 at 02: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. – Wolfpack'08 Jul 05 '13 at 22:15
  • 1
    That was helpfull. But can anybody expalin why and how it gets things working ? – Tarun Gupta Dec 27 '13 at 07: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. – Michael Dillon Dec 27 '13 at 08:01
  • Thanks @MichaelDillon. "ln" means giving a another link for the a path. right? – Tarun Gupta Dec 28 '13 at 04:52
  • Helped me with similar issue with composer on an EC" instance. Installed globally `composer self-update` gave permissions error `[ErrorException] rename(/home/ec2-user/.composer/cache/composer-temp.phar,/usr/local/bin/composer): Permission denied` BUT sudo composer self-update couldn't find composer. Adding link as suggested worked a treat. – Tofuwarrior May 13 '15 at 09:10
  • 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 '15 at 12:45
  • Woah! Saved my life (Y) – softvar Jan 24 '16 at 21:10
  • I wish I could upvote this twice, I've been using it several times within 2 days – christophetd Mar 22 '16 at 16:16
  • `sudo ln -s $(which node) /usr/bin/node` if you're afraid of typos and you use nvm like me. I also added the `sudo ln -s /path/to/lib/node_modules /usr/lib/node_modules` and `sudo ln -s $(which npx) /usr/bin/npx` symlinks to be safe. – Stardust Feb 02 '21 at 05:56
33

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
Jack Frost
  • 2,121
  • 3
  • 16
  • 21
27

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!

Jeff Pal
  • 1,036
  • 11
  • 18
  • 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](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html) – NULL pointer Aug 14 '18 at 06:10
12

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
Shripad Krishna
  • 10,045
  • 4
  • 50
  • 63
5

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

sudo `which node`
Pavel Zubkou
  • 756
  • 8
  • 13
5

try the following:

export PATH=$PATH:/usr/local/bin
sudo node --version
Amro
  • 121,265
  • 25
  • 232
  • 431
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

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

Will Voelcker
  • 256
  • 3
  • 5
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

Jack Murphy
  • 2,643
  • 21
  • 41
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

shubham
  • 1,119
  • 1
  • 11
  • 26
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)"
Baimyrza Shamyr
  • 339
  • 1
  • 5
  • 15
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

slimgera00
  • 48
  • 8
-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.

bdavis
  • 31
  • 4