Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Do I need sudo to install node?

To summarize, you can use the following steps to update your local OS X machine to allow you to use global Node. js modules and different versions of node without using sudo . brew install node , or apt-get install node or whatever you need to do to get node up on your machine.

How will you resolve the error npm command not found?

The npm command not found error js server, which you can download from the nodejs.org website. Once you downloaded and installed Node. js, open the terminal and run the npm -v command. Once you see the npm version, you should be able to run the npm install command again.


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.


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

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!


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

try the following:

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

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

sudo `which node`

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