6

I am trying to call an installation of node.js on a remote server running Ubuntu via SSH. Node has been installed via nvm.

SSHing in and calling node works just fine:

user@localmachine:~$ ssh user@remoteserver
(Server welcome text)
user@remoteserver:~$ which node
/home/user/.nvm/v0.10.00/bin/node

However if I combine it into one line:

user@localmachine:~$ ssh user@remoteserver "which ls"
/bin/ls
user@localmachine:~$ ssh user@remoteserver "which node"

No sign of node, so I tried sourcing .bashrc and waiting 10 seconds:

user@localmachine:~$ ssh user@remoteserver "source ~/.bashrc; sleep 10; which node"

Only node seems affected by this. One thing I did notice was that if I ssh in and then check which shell I'm in it says -bash whilst if I ssh direct it gives me /bin/bash. I tried running the commands inside a bash login shell:

user@localmachine:~$ ssh user@remoteserver 'bash --login -c "which node"'

Still nothing.

Basically my question is: Why isn't bash finding my node.js installation when I call it non-interactively from SSH?

2 Answers 2

7

Another approach is to run bash in interactive mode with the -i flag:

user@localmachine:~$ ssh user@remoteserver "bash -i -c 'which node'"
/home/user/.nvm/v0.10.00/bin/node
Sign up to request clarification or add additional context in comments.

Comments

3
$ ssh user@remoteserver "which node"

When you run ssh and specify a command to be run on the remote system, ssh by default doesn't allocate a PTY (pseudo-TTY) for the session. Not having a TTY causes your remote shell process (ie, bash) to initialize as a non-interactive session instead of an interactive session. This can alter how it interprets your initialization files--.bashrc, .bash_profile, and so on.

The actual problem is probably that the line which adds /home/user/.nvm/v0.10.00/bin to your command PATH isn't executing for non-interactive sessions. There are two ways to resolve this:

  1. Find the command in your initialization file(s) which adds /home/user/.nvm/v0.10.00/bin to your command path, figure out why it's not running for non-interactive sessions, and correct it.

  2. Run ssh with the -t option. This tells it to allocate a PTY for the remote session. Or add the line RequestTTY yes to your .ssh/config file on the local host.

3 Comments

I've run it as ssh -t user@remoteserver "which node", but it still doesn't find it. Adding RequestTTY to the .ssh/config file seems to have no effect either.
I've also tried ssh -t user@remoteserver "bash --rcfile ~/.bashrc -c 'which node'" which doesn't seem to have an effect.
I've just tried: ssh -t user@remoteserver 'export NVM_DIR="/home/user/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; which node' and that's working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.