Skip to content
Advertisement

which node gives quite strange result

Perhaps this seems silly, but when I run which node today it gives me something quite different as

/home/hearen/xxx/xxx/node-v0.10.28/bin/node

instead of something as

/usr/local/bin/node

However when I run sudo which node:

/usr/local/bin/node

I am wondering how this could happen?

My Silly Questions

  • how which command works to locate the path for the command;
  • which node exactly selected based on the path? how?
  • in my case, how can I use /usr/local/bin/node instead of the current weird one /home/hearen/xxx/xxx/node-v0.10.28/bin/node? just resetting the path will do the trick?
  • why sudo which node gives me the right result? anything I missed?

Any help will be appreciated 🙂

More Details

nvm

$ nvm --version
0.33.0


$ nvm list
         v6.6.0
         v9.1.0
->     v10.10.0
         system
default -> 6.6.0 (-> v6.6.0)
node -> stable (-> v10.10.0) (default)
stable -> 10.10 (-> v10.10.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.16.0 (-> N/A)
lts/carbon -> v8.15.0 (-> N/A)
lts/dubnium -> v10.15.1 (-> N/A)

echo $PATH

/home/hearen/.nvm/versions/node/v10.10.0/bin:/home/hearen/XXX/XXX/oracle-jdk-1.8.0_121/bin:/home/hearen/XXX/XXX/apache-maven-3.3.1/bin:/home/hearen/XXX/XXX/node-v0.10.28/bin:/home/hearen/XXX/XXX/node-v0.10.28/bin:/home/hearen/XXX/XXX/mysql-5.6.39/bin:/home/hearen/XXX/XXX/yosolo-0.0.1-SNAPSHOT:/opt/mozjpeg/bin:/home/hearen/XXX/XXX/git-lfs-2.4.0:/home/hearen/XXX/WorkSpace/Develop/bin:/home/hearen/bin:/usr/local/bin:/usr/bin:/bin:/sbin:/home/hearen/.local/bin:/opt/jdk1.8.0_144/bin:/home/hearen/XXX/XXX/oracle-jdk-1.8.0_121/bin:/home/hearen/XXX/XXX/apache-maven-3.3.1/bin:/home/hearen/XXX/XXX/node-v0.10.28/bin:/home/hearen/XXX/XXX/node-v0.10.28/bin:/home/hearen/XXX/XXX/mysql-5.6.39/bin:/home/hearen/XXX/XXX/yosolo-0.0.1-SNAPSHOT:/opt/mozjpeg/bin:/home/hearen/XXX/XXX/git-lfs-2.4.0:/home/hearen/XXX/WorkSpace/Develop/bin:/home/hearen/bin:/usr/local/bin:/usr/bin:/bin:/sbin:/home/hearen/bin:/home/hearen/.local/bin:/opt/jdk1.8.0_144/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Advertisement

Answer

The way Node Version Manager and similar software packages work is by putting a copy of and a few wrapper scripts and/or s in your user home directory (under ~/.nvm by default) and prepending the directory containing those scripts to your PATH environment variable. That is usually done by modifying your shell startup script (i.e. ~/.bashrc or other files depending on your selected shell and current configuration).

The reason it shows a different path when you run which node with sudo is that you most likely have node.js installed globally and the directory containing those wrapper scripts is not added to root user’s PATH environment variable.

You can confirm this by comparing the output of the following commands:

echo $PATH

And

sudo echo $PATH

To use the globally installed version of node, you can remove the line that sources nvm.sh from your shell startup script (and logout and log back in – this will disable nvm permanently) or reset your PATH environment variable.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement