which node returns path to node, but not if I run in su - user -s /bin/bash -c 'which node'
0
votes
1
answer
44
views
When I run
which node
the command works, but if I run su - username -s /bin/bash -c 'which node'
it returns empty. I have tried everything under the sun and can not get it to work.
I even tried su - username -s /bin/bash -c 'source ~/.bashrc ; which node'
-shellsession
root@vps-151c25b9:~# which node
/root/.nvm/versions/node/v20.18.0/bin/node
root@vps-151c25b9:~# su - username -s /bin/bash -c 'which node'
root@vps-151c25b9:~#
root@vps-151c25b9:~# su - username -s /bin/bash
username@vps-151c25b9:~$ which node
/home/username/.nvm/versions/node/v20.18.0/bin/node
username@vps-151c25b9:~$
What am I doing wrong? Why doesn't node show up when run with su ?
**Edit:** I just noticed, when I run
-sh
su - username -s /bin/bash -c "echo $PATH"
I get the path of root
, not of the user *username
*. Although root
also has nvm
& node
and it doesn't find that either.
**Edit 2:** I tried single quotes , so it doesn't expand $PATH
and got my correct path including nvm dir:
-sh
su username -c 'echo $PATH'
but when I try
-sh
su username -c 'source ~/.bashrc ; which node'
it still is empty.
**Edit 3:** I tried
-sh
su username -c '. ~/.profile ; which which'
(lol) and it returned /usr/bin/which
, so the command works, just not for node.
**Answer:** I ran the following and it worked! I had to load NVM manually.
-shellsession
root@vps-151c25b9:~# su - username -c 'export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" ; [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" ; which node'
/home/username/.nvm/versions/node/v20.18.0/bin/node
Thank you everyone for your help :)
Asked by tehnutz
(1 rep)
Nov 6, 2024, 09:24 AM
Last activity: Nov 10, 2024, 05:59 AM
Last activity: Nov 10, 2024, 05:59 AM