node.js - 如何通过 ssh 运行 npm

标签 node.js bash

按照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/

相关文章:

node.js - 如何访问 netlify-lambda 中的 gatsby 环境变量

javascript - 如何防止子 Node 进程被父 Node 进程杀死?

regex - BASH KDialog 多文件输入 - 在特定空间拆分字符串

linux - 从我的 bash 脚本调用另一个 bash 脚本,给它一些输入值

linux - Unix 备份脚本

bash,bc modulo 不适用于 -l 标志

bash - 应该在 .bash_profile 文件中更改什么才能完全删除/卸载 Conda/Anaconda?

javascript - Waterline 中的并行写入访问

javascript - 如何从Json中删除特殊字符而不解析

javascript - NodeJS 阻塞读取多个文件并返回快速响应