0

I've installed some of the software under /opt folder by compiling. I've added their "bin" directory to the path inside "/etc/environment". I can also use them as a regular user. For eg: NodeJS 10.15.1 has been installed in /opt and giving node -v outputs v10.15.1.

Similarly if I run the same with sudo,

sudo node -v

I'm getting sudo: node: command not found. If I become root by sudo -s and then execue node -v, I'm getting

Command 'node' not found, but can be installed with:

apt install nodejs

The reason I want to use "sudo" is, I've installed "Angular" using "npm". And at certain times, ng serve not reflecting new code changes automatically (which is default behavior of "ng serve" command).

So as pointed out in this post, I want to run this command with "sudo".

Why can't sudo find the executable when it has been added to "PATH"?

learner
  • 571
  • Did you add it to the root users PATH as well as you local user? What happens if you run it with sudo but specify the full path I.e. sudo /opt/path/to/node ? – rohtua Mar 10 '19 at 08:08
  • @rohtua, running with fully qualified path will execute the program whether it's with "sudo" or not. – learner Mar 10 '19 at 08:15
  • Try editing the root users .bashrc file in their home directory and adding the following to the end of the file, export PATH=$PATH:/opt/path/to/node that's the way I add applications or scripts to my path if they're installed in a custom location. You can do the same for local user as well. You'll have to close the running terminal session and start a new one for the changes to take effect. – rohtua Mar 10 '19 at 08:25
  • Doesn't sudo reset the path to safe values ? Try to run sudo echo $PATH – Soren A Mar 10 '19 at 10:43
  • @SorenA you'd need something more like sudo sh -c 'echo $PATH' (so that $PATH doesn't get expanded by the local shell). However sudo searches its own secure_path rather than root's PATH AFAIK – steeldriver Mar 10 '19 at 11:41
  • @SorenA, executing "sudo echo $PATH" showed contents of "/etc/environment" file as told by steeldriver. – learner Mar 11 '19 at 08:02
  • @steeldriver, how to make either "sudo" command or "root" user to consider all the paths mentioned inside "/etc/environment" file? – learner Mar 11 '19 at 08:03
  • @rohtua, I added node location to .bashrc file. It started executing after I changed to root user completely by "sudo -s", however, got some exception (UnhandledPromiseRejectionWarning: Error: Exited with code 3 at ChildProcess.cp.once.code). But just prepending "sudo" command ("sudo /opt/node-v10.15.1-linux-x64/bin/ng serve --open"), still showing "/usr/bin/env: ‘node’: No such file or directory". But one thing I don't understand is why paths in "/etc/environment" file is ignored? Why should I do these extra steps? – learner Mar 11 '19 at 08:05
  • 2

0 Answers0