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

Solution 1:

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:

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:

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:

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