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?
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
commandis specified, it is executed on the remote host instead of a login shell.
-tForce 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.bashrcand~/.bashrc, if these files exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With