dev-sidecar icon indicating copy to clipboard operation
dev-sidecar copied to clipboard

打开npm直接报错Api invoke error Error invoking remote method 'apiInvoke': Error: /bin/sh: npm: command not found

Open ammondu opened this issue 1 year ago • 19 comments

  • [x] 你是否在现有 Issue列表 中搜索过相同问题,但未找到?

Ⅰ. 请说明操作系统及DS的版本号:

  1. 操作系统:macos 14.7.2
  2. DS版本号:2.0.0-RC1

Ⅱ. 问题描述:

打开npm 直接报错 Api invoke error Error invoking remote method 'apiInvoke': Error: /bin/sh: npm: command not found

Ⅲ. 期望的结果:

解决bug

Ⅳ. 如何复现问题?

直接打开npm就会提示。

Ⅴ. 请提供相关的错误日志,尽可能的详细:(日志文件在 ${user.home}/.dev-sidecar/logs/ 目录下)

[2025-01-02T10:49:06.864] [INFO] core - shell: npm config list --json
commands: npm config list --json 
   error: Error: Command failed: npm config list --json
/bin/sh: npm: command not found
  cmd: 'npm config list --json'

ammondu avatar Jan 02 '25 02:01 ammondu

看下环境变量有没有配置吧。

wangliang181230 avatar Jan 02 '25 04:01 wangliang181230

me too... 什么鬼: Api invoke error Error invoking remote method 'apiInvoke': Error: 'npm' �����ڲ����ⲿ���Ҳ���ǿ����еij��� ���������ļ���

Ericlyclyclyc avatar Jan 04 '25 15:01 Ericlyclyclyc

cmd打开npm正常啊

看下环境变量有没有配置吧。

Ericlyclyclyc avatar Jan 04 '25 15:01 Ericlyclyclyc

遇到了类似的问题。

在ubuntu 22.04 下, node 和npm 均以安装好(通过nvm安装),且终端中node -v 和 npm -v 均能正确输出版本信息。

终端和dev-sidecar 以相同用户启动。

当dev-sidecar 选中npm 代理时,输出异常“Api invoke error Error invoking remote method 'apiInvoke': Error: /bin/bash: 行 1: npm: 未找到命令”

lanhao945 avatar Jan 06 '25 03:01 lanhao945

估计是权限问题,你们把ds的权限提高一下试试。

wangliang181230 avatar Jan 06 '25 03:01 wangliang181230

image

ammondu avatar Jan 06 '25 03:01 ammondu

你们尝试下把npm的完整路径配置到 ds 的 npm命令里试一下?

wangliang181230 avatar Jan 06 '25 03:01 wangliang181230

如: 图片

wangliang181230 avatar Jan 06 '25 03:01 wangliang181230

我mac下确实没有这个 /bin/sh: npm: command not found

ammondu avatar Jan 06 '25 03:01 ammondu

我在我mac中brew install npm 问题解决。你们可以继续讨论。

ammondu avatar Jan 06 '25 04:01 ammondu

通过查找npm 路径得到:

home/username/.nvm/versions/node/v23.5.0/bin/npm 然后按照要求,在devsidecar中配置npm 路径,重启后,尝试开启npm 代理: Error invoking remote method 'apiInvoke': Error: /usr/bin/env: "node": 没有那个文件或目录

从表现来看,不像是权限,更像是,devsidecar中将node 的路径(或者npm路径)写得相对固定?

lanhao945 avatar Jan 06 '25 04:01 lanhao945

这是devsidecar core.log 中关于node 的部分日志:

===>
commands: npm config set proxy=http://127.0.0.1:31181 
   error: Error: Command failed: npm config set proxy=http://127.0.0.1:31181
/bin/bash: 行 1: npm: 未找到命令

    at ChildProcess.exithandler (node:child_process:408:12)
    at ChildProcess.emit (node:events:526:28)
    at maybeClose (node:internal/child_process:1092:16)
    at Socket.<anonymous> (node:internal/child_process:451:11)
    at Socket.emit (node:events:526:28)
    at Pipe.<anonymous> (node:net:687:12) {
  killed: false,
  code: 127,
  signal: null,
  cmd: 'npm config set proxy=http://127.0.0.1:31181'
} 
<===

lanhao945 avatar Jan 06 '25 04:01 lanhao945

我在我mac中brew install npm 问题解决。你们可以继续讨论。

ubuntu中通过apt install 应该能解决这个问题。但是这里主要是用到了nvm,是希望通过nvm 来控制node 的版本。估计是引入了nvm 导致了逻辑不兼容。

lanhao945 avatar Jan 06 '25 04:01 lanhao945

可能是使用了像zsh类似的shellnvm只会把路径注册到.zshrc中,导致在原始的sh找不到npm的路径,后面我们可以考虑使用 tinyexec 替换内部实现,它处理了环境变量的问题

starknt avatar Jan 06 '25 13:01 starknt

...我是windows11...

lyc


发件人: star knight @.> 发送时间: 2025年1月6日 21:57 收件人: docmirror/dev-sidecar @.> 抄送: Ericlyclyclyc @.>; Comment @.> 主题: Re: [docmirror/dev-sidecar] 打开npm直接报错Api invoke error Error invoking remote method 'apiInvoke': Error: /bin/sh: npm: command not found (Issue #427)

可能是使用了像zsh类似的shell,nvm只会把路径注册到.zshrc中,导致在原始的sh找不到npm的路径

― Reply to this email directly, view it on GitHubhttps://github.com/docmirror/dev-sidecar/issues/427#issuecomment-2573165474, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BHAI2AO6KHEQORQJRYF4TSL2JKDVPAVCNFSM6AAAAABUPHSL46VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKNZTGE3DKNBXGQ. You are receiving this because you commented.Message ID: @.***>

Ericlyclyclyc avatar Jan 06 '25 14:01 Ericlyclyclyc

在ubuntu 22.04 下,尝试继续保留 nvm 并使用DS,最后发现:

在路径/etc/profile 中增加nvm 相关的配置,并重启, DS 中点击npm 代理时,不再报错.

其中增加的配置,来自于安装脚本安装后,原本存在于用户目录下~/.bashrc 尾部新增的部分:

export NVM_DIR="$HOME/.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

但是这种做法混淆了系统软件和用户软件,感觉只能适用于个人主机。

希望 DS 后续能修复这里的BUG。感谢

lanhao945 avatar Jan 07 '25 01:01 lanhao945

这行代码,Mac 上 shell.getSystemShell() 得到的是 sh 而不是 zsh。所以仿照 Linux 系统指定 shell 为 zsh 即可。

peasoft avatar Jan 21 '25 10:01 peasoft

well,在Windows上,现在是不会自己打开,需要手动操作了

lyc


发件人: Pea Soft @.> 发送时间: 2025年1月21日 18:55 收件人: docmirror/dev-sidecar @.> 抄送: Ericlyclyclyc @.>; Comment @.> 主题: Re: [docmirror/dev-sidecar] 打开npm直接报错Api invoke error Error invoking remote method 'apiInvoke': Error: /bin/sh: npm: command not found (Issue #427)

见 这行代码https://github.com/docmirror/dev-sidecar/blob/3ae628287037047e176ee7277234690de561242c/packages/core/src/shell/index.js#L23,Linux 上 shell.getSystemShell() 得到的是 sh 而不是 bash。所以 Linux 系统需要特殊处理:

if (isLinux() && hasFile("/bin/bash")) { return shell.getSystemShell().exec(["/bin/bash", "-c", ...cmds], args) } else { return shell.getSystemShell().exec(cmds, args) }

― Reply to this email directly, view it on GitHubhttps://github.com/docmirror/dev-sidecar/issues/427#issuecomment-2604390225, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BHAI2AISD3JIMEM4REREYBD2LYRTLAVCNFSM6AAAAABUPHSL46VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMBUGM4TAMRSGU. You are receiving this because you commented.Message ID: @.***>

Ericlyclyclyc avatar Jan 21 '25 10:01 Ericlyclyclyc

然后,发现关闭代理(点圆型按钮)在点开(也是点圆形按钮)就会自动打开了

Ericlyclyclyc avatar Jan 23 '25 13:01 Ericlyclyclyc