node-ssh icon indicating copy to clipboard operation
node-ssh copied to clipboard

Different results with node-ssh and my terminal ssh connection

Open leobgm opened this issue 4 years ago • 11 comments

Hi I'm using node-ssh to run this command in a remote machine

nvm install node

When my code sends this command via ssh.execCommand('nvm install node') the response is bash: nvm: command not found

But when I personally connect to that server via my terminal, it just runs fine. Without any errors. why?

leobgm avatar Dec 07 '21 10:12 leobgm

Hey! Fly-by comment, can you do "echo $PATH" for both (without the quotes) and observe the difference?

some shell configurations behave differently when connected over a non-xterm shell or something, just want to make sure that's not the case

steelbrain avatar Dec 07 '21 10:12 steelbrain

Sure.

This is the result in my own terminal /root/.nvm/versions/node/v17.2.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

And this is what the app gets /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

leobgm avatar Dec 07 '21 10:12 leobgm

That explains it! Your shell configuration is conditionally adding nvm to the env variables, you'll have to look up or see how to fix that

once the env variables match, the nvm command should work for both

steelbrain avatar Dec 07 '21 10:12 steelbrain

Thanks man. Yeah looks like this is the case. Why node-ssh acts differently? Isn't there a way to make its behavior like normal Linux terminal?

leobgm avatar Dec 07 '21 11:12 leobgm

There's too many variables at play here so I cannot say for sure why it's this way. When setting up nvm, it injects some lines into your shell profile like .bash_profile or .bashrc. I'd recommend you look it over, see that things are getting initialised properly.

steelbrain avatar Dec 07 '21 13:12 steelbrain

If you want to understand the problem better, Google differences between login and non-login shell. With execCommand node-ssh acts as a non-login shell, just as if you ran ssh HOST "nvm install node".

Easy workaround to this would be to load your profile like .bash_profile or .bashrcbefore the command:

ssh.execCommand('source ~/.bash_profile; nvm install node');

maquak avatar Dec 12 '21 23:12 maquak

If you want to understand the problem better, Google differences between login and non-login shell. With execCommand node-ssh acts as a non-login shell, just as if you ran ssh HOST "nvm install node".

Easy workaround to this would be to load your profile like .bash_profile or .bashrcbefore the command:

ssh.execCommand('source ~/.bash_profile; nvm install node');

Thanks. I tried that. But didn't work.

leobgm avatar Dec 24 '21 16:12 leobgm

Hello, @soroushBgm, i had this issue today.

I noticed there is a line in my ~/.bashrc at the server machine:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

I just commented this return so it's right now something like:

# If not running interactively, don't do anything
# [ -z "$PS1" ] && return

And everything works fine for me now.

TokiSeven avatar Jan 14 '22 20:01 TokiSeven

Having the same issue now. Bit of a newbie but I'm having to use this for school, anyone have a proper solution yet?

k1ngofpentacles avatar Dec 08 '22 20:12 k1ngofpentacles

Modifying the /etc/profile file does not work properly,maybe you can try ln -s /nodejs/bin/nvm /usr/local/bin

wangjue666 avatar Mar 21 '23 03:03 wangjue666

I managed to change the version to the desired one using: ssh <username>@<host> "export PATH=\$HOME/.nvm/versions/node/v16.17.0/bin:\$PATH && node -v"

I think, you can use part of it for node-ssh

smeleshkin avatar Sep 02 '23 09:09 smeleshkin