How to run npm through ssh

Solution 1:

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.