从 SSH 触发时,Npx 返回未找到命令 [英] Npx returns command not found when triggered from SSH
问题描述
我在 SO 上发现了很多类似的问题,但遗憾的是它们与我的问题无关.
I found quite a lot of similar questions on SO, but they sadly aren't related to my issue.
我安装了带有 NVM 的 Node.
I have Node installed with NVM.
$ node -v
v14.16.0
$ npm -v
7.7.6
$ npx -v
7.7.6
我正在编写一个部署脚本,它将从我们的存储库运行 git pull,然后(除其他外)编译资产.如果这有帮助,工作环境是 Laravel 8,但我认为这与我的问题无关.
I'm writing a deployment script that will run git pull from our repository, then (among other things) compile the assets. If this helps, the work environment is Laravel 8, but I don't think it's related to my issue.
运行 npm install &&npm run prod
以远程服务器上的用户身份登录时将完美运行.但是,通过 SSH 运行相同的命令会返回以下内容:
Running npm install && npm run prod
while logged as the user on the distant server will work flawlessly. However running the same command via SSH returns the following:
jaeger@local-computer~$ ssh deploy@distant-server-with-nvm-node-npm-npx '/bin/sh -c "node -v && npm -v && npx -v && exit"'
Connecting...
v14.16.0
7.7.6
/bin/sh: 1: npx: not found
所以……我迷路了.我已经看到有关 node 和/或 npm 被识别的问题,但没有关于 npx,我需要它与 Laravel 混合.当然,从远程服务运行上述注释会完美运行.
So... I'm lost. I've seen questions about node and/or npm being recognized, but none about npx, and I need it with Laravel mix. Of course, running the above comment from the distant service will work perfectly.
- 将
PermitUserEnvironment yes
添加到/etc/ssh/sshd_config,然后重新启动 ssh - 通过
npm i -g npx
全局安装 npx,被 nvm 拒绝,说明 npx 已经存在. - 重新安装 nvm 和节点.
- 在
/home/myuser/.nvm/versions/v14.16.0/bin/npx
和/usr/local/bin
之间创建符号链接
- Adding
PermitUserEnvironment yes
to /etc/ssh/sshd_config, then restarting ssh - Installing globally npx via
npm i -g npx
, got denied by nvm, stating that npx already exists. - Réinstalling nvm and node.
- Create a symlink between
/home/myuser/.nvm/versions/v14.16.0/bin/npx
and/usr/local/bin
我还能尝试什么?提前致谢
What else can I try? Thank you in advance
推荐答案
最有可能的是,nvm
已将自身安装在您的 .bashrc
中,但是您触发了一个不同的shell (sh
).即使 /bin/sh
指向 bash,也会禁用 rcfile - 请参阅下面的说明.直接使用bash
.
Most likely, nvm
has installed itself in your .bashrc
, but you're triggering a different shell (sh
). Even if /bin/sh
points to bash, this disables using the rcfile - see explanation below. Use bash
directly.
请注意 bash 手册页的摘录:--norc
Note this excerpt from bash manpage: --norc
如果 shell 是交互式的,不要读取和执行系统范围的初始化文件/etc/bash.bashrc 和个人初始化文件 ~/.bashrc.如果 shell 作为 sh 调用,则默认情况下此选项处于启用状态.
Do not read and execute the system wide initialization file /etc/bash.bashrc and the personal initialization file ~/.bashrc if the shell is interactive. This option is on by default if the shell is invoked as sh.
这意味着除了通过 - 指定命令外,您还需要使用标志
.-i
强制 bash 作为 交互式 shell 运行c
This means you'll need to force bash to be run as an interactive shell using the flag -i
, in addition to specifying the command via -c
.
这篇关于从 SSH 触发时,Npx 返回未找到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!