Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The problem with running version managed Node as a remote SSH script

My question is about nvm but it may relate to other Node version manages like n or nvs.

You probably a;ready know that you can run remote programs with SSH like this:

ssh user@server COMMAND

For example the command can be the Node.js script:

ssh user@devserver 'node ~/getstats'

The problem is that it will not work for Node that was installed using nvm. Why? Because node is actually an alias to something like /home/user/.nvm/versions/node/v12.1.0/bin/node. The alias is installed in ~/.bashrc which is run when you login with SSH. But when you execute remote command with ssh SERVER COMMAND environment scripts are not run because the shell runs in a restricted mode.

One work around is to create ~/node which containts /home/monitor/.nvm/versions/node/v12.1.0/bin/node * and is executable, then you can do ssh SERVER './node SCRIPT'. But this is not perfect because once you upgrade Node the path will change and you will need to update this file as well.

What would be the recommended way to solve the problem with running version managed Node as a remote SSH script?

like image 905
exebook Avatar asked Sep 12 '19 07:09

exebook


1 Answers

Try this:

ssh user@devserver '. ~/.nvm/nvm.sh && node ~/getstats'

Note that by default, .bashrc in Ubuntu 18.04 has the following:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
like image 52
Andrew Avatar answered Nov 05 '22 12:11

Andrew