Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run npm through ssh

Tags:

bash

node.js

After installing npm according to https://nodesource.com/blog/installing-node-js-tutorial-using-nvm-on-mac-os-x-and-ubuntu/ , 3 lines were added at the end of ~/.bashrc to load nvm tools.

When connecting to that server with ssh, npm --version outputs 5.5.1 as expected.

But when running remotely :

ssh server /bin/bash -l -c "npm --version"

I get :

--version: npm: command not found

What is the correct way to have npm available when running it remotely in one line?

like image 896
samb Avatar asked Oct 23 '25 15:10

samb


1 Answers

The problem is as you may suspect, your .bashrc is not being sourced. You had the right idea by adding the -l flag but what you really need in this case is the -i flag to spawn an interactive shell, in-turn sourcing your .bashrc prior to command execution.

ssh -t "server" 'bash -i -c "npm --version"'

man ssh

If command is specified, it is executed on the remote host instead of a login shell.

-t Force pseudo-terminal allocation.

man bash

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.

like image 86
Travis Clarke Avatar answered Oct 26 '25 03:10

Travis Clarke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!