-2

I remember I was able to use something like sudo ${which npm} run xxx when I needed root privilege to run npm. What is it?

1 Answers1

0

With bash you can do

sudo "$(which npm)" run ...

# or

sudo "`which npm`" run ...
joeytwiddle
  • 484
  • 4
  • 7
  • yes but it will say node not found – Aero Windwalker Jun 05 '20 at 07:19
  • @AeroWindwalker By default `sudo` will pass the `PATH` variable down to the child process, but sudo's security policy can override that. Assuming `which node` works before running sudo, you may need to add `Defaults env_keep += "PATH"` to your sudoers file (by running `sudo visudo`). See [here](https://superuser.com/questions/232231/how-do-i-make-sudo-preserve-my-environment-variables) and sudo's [man page](https://www.sudo.ws/man/1.8.13/sudoers.man.html). If that works you may drop the `which` trick entirely. – joeytwiddle Jun 05 '20 at 07:46