Reputation: 1745
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?
Upvotes: 4
Views: 11023
Reputation: 11
I have my nvm.sh file in /etc/profile.d with the following content:
export NVM_DIR = "/ usr / local / nvm"
[-s "$ NVM_DIR / nvm.sh"] && \. "$ NVM_DIR / nvm.sh" # This loads nvm
[-s "$ NVM_DIR / bash_completion"] && \. "$ NVM_DIR / bash_completion" # This loads nvm bash_completion
So to connect by ssh from another server I can do it by increasing previously . /etc/profile.d/nvm.sh
, that is:
ssh -t "server" ". /etc/profile.d/nvm.sh && npm -v"
Upvotes: 0
Reputation: 3420
Please refer to this question: https://apple.stackexchange.com/questions/12993/why-doesnt-bashrc-run-automatically
It has both a solution and an explanation for the root cause of your problem. Normally you shouldn't have to specify anything at the ssh
level - shell initialization is to supposed to be automatic and ssh
is supposed to open an interactive shell.
Upvotes: 0
Reputation: 6661
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.
Upvotes: 9