按照https://nodesource.com/blog/installing-node-js-tutorial-using-nvm-on-mac-os-x-and-ubuntu/安装npm后,在~/.bashrc
末尾添加了3行来加载nvm工具。
使用 ssh 连接到该服务器时,npm --version
按预期输出 5.5.1
。
但是远程运行时:
ssh server /bin/bash -l -c "npm --version"
我明白了:
--version: npm: command not found
在一行中远程运行 npm 时,使 npm 可用的正确方法是什么?
最佳答案
问题正如您可能怀疑的那样,您的 .bashrc
没有被获取。通过添加 -l
标志,您的想法是正确的,但在这种情况下,您真正需要的是 -i
标志来生成一个交互式 shell,进而在命令执行之前获取 .bashrc
。
ssh -t "server" 'bash -i -c "npm --version"'
人ssh
If
command
is specified, it is executed on the remote host instead of a login shell.
-t
Force pseudo-terminal allocation.
男人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.
关于node.js - 如何通过 ssh 运行 npm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47777368/