通过sudo运行npm(不是以root用户身份,也不是以自己的用户身份)不能在root或者self-user所在的地方findnpm

我有一个makefile,我用sudo make install我的应用程序的一部分,通常需要根安装。 不幸的是,这导致了一个奇怪的情况, sudo不能访问npm ,而root和我的用户都可以访问:

自:

 $ which npm /home/self/.nvm/versions/node/v5.9.1/bin/npm 

根:

 $ sudo su - root@theserver~# which npm /root/.nvm/versions/node/v5.9.1/bin/npm 

出口

$ sudo其中npm $(这是一个空行,sudo哪个npm没有结果)

我如何解决这个差异? 我错过了npm生态系统,可以让我做到这一点吗?

我正在运行Ubuntu 14.04 LTS:

 cat /proc/version cat /etc/issue uname -a Linux version 3.13.0-44-generic (buildd@lamiak) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014 Ubuntu 14.04.4 LTS \n \l Linux theserver 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux 

标准用户和root用户的PATH和HOME。

 user@server:~$ cat ~/.profile # ~/.profile: executed by the command interpreter for login shells. # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists. # see /usr/share/doc/bash/examples/startup-files for examples. # the files are located in the bash-doc package. # the default umask is set in /etc/profile; for setting the umask # for ssh logins, install and configure the libpam-umask package. #umask 022 # if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi user@server:~$ sudo echo $PATH $HOME /home/user/.nvm/versions/node/v5.9.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games /home/user user@server:~$ echo $PATH $HOME /home/user/.nvm/versions/node/v5.9.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games /home/user user@server:~$ sudo su root@server:/home/user# echo $PATH $HOME /root/.nvm/versions/node/v5.9.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games /root root@server:/home/user# cat ~/.profile # ~/.profile: executed by Bourne-compatible login shells. if [ "$BASH" ]; then if [ -f ~/.bashrc ]; then . ~/.bashrc fi fi mesg n 

这可能是因为你的sudoers(5)文件中有一个env_reset指令。

默认情况下,启用env_reset选项。 这会导致命令执行一个新的,最小的环境…

最小的环境不包含PATH。 sudoers(5)手册页的“ Command Environment部分提供了大量相关信息供您阅读。