使用 WSL2 与 Windows 上安装的应用程序交互

2024-02-16

我已启动并运行 WSL2。在Windows级别我安装了node.js,现在我想运行npm install snowflake-sdk从Ubuntu级别。终端退货-bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory而 PowerShell 工作正常。

问题是:是否可以使用 WSL2 的终端与 Windows 级别安装的应用程序进行交互?


Note:

  • The more typical scenario is to install Node.js and npm on the Linux side, as per the official instructions https://learn.microsoft.com/en-us/windows/nodejs/setup-on-wsl2Thanks, SeaDude https://learn.microsoft.com/en-us/windows/nodejs/setup-on-wsl2. If you then need to call from Windows, you can use
    wsl npm ..., but note that cross-file-system calls are generally discouraged in WSL2 https://learn.microsoft.com/en-us/windows/wsl/compare-versions#performance-across-os-file-systems.

harzvor 的有用答案 https://stackoverflow.com/a/64742430/45375提供了良好的背景信息。

It is可以拨打电话npm from a Windows安装Node.js 副本来自 WSL2工作,虽然不明显而且有点麻烦- 尽管您可以将解决方法包装在自定义函数或脚本中:

# From WSL2; note the required use of '.exe'
node.exe 'c:/Program Files/nodejs/node_modules/npm/bin/npm-cli.js' install snowflake-sdk

上面的代码依赖于 Windows 上安装在默认位置的 Node.js,调用npm 间接地,使用与 CLI 入口点基本相同的技术在 Windows 上, the npm.cmd 批处理文件, uses.

Note:

  • 为了瞄准一个Windows从 WSL 可执行文件,您必须包含文件扩展名明确地, 例如node.exe而不是node.

  • 没有它,例如和npm,Linux 子系统寻找无扩展文件中的该名称$PATH文件夹,它的作用是not work.

    • Such a file actually does exist even on Windows, but it is a shebang-based script designed for Unix-like platforms, and basically just dead weight in a Windows installation; the Linux subsystem still tries to execute it, but fails, because it uses CRLF (\r\n) newlines rather than the expected LF-only (\n) newlines. As a result, the unexpected CR that follows #!/bin/sh (represented as ^M in the error message) is considered a part of the executable path, so the invocation fails.[1]

    • Even fixing that problem manually (converting to LF-only newlines) doesn't help, however, because of how the shell script constructs the full path to the npm-cli.js entry point: it expresses it as an UNC path that tries to target the Windows file-system location via the WSL file-system - e.g.,
      \\wsl$\Ubuntu-20.04\mnt\c\... - which is explicitly disallowed.[2]

  • 呼叫batch file, npm.cmd明确地,也不起作用 - 既不直接工作,也不与cmd.exe /c npm.cmd ...:

    • 直接调用*.cmd文件模糊地失败 - 不确定这是否是 Windows 10 20H2 的错误。
    • Via cmd.exe /c它也失败了,因为cmd.exe不支持 UNC 路径作为当前目录,并且 WSL2 内部当前目录路径始终表示为 1(例如,\\wsl$\Ubuntu-20.04\home\jdoe\project\some-project).
      cmd.exe默认为Windows目录在这种情况下,将软件包安装到当前 WSL 目录(项目)将不起作用)。
  • 提供 npm CLI 主窗口的完整 Windows 原生路径.js文件到node.exe,如上所示,按预期工作,因为 Node.js 似乎以其他方式处理引用的 UNC 路径WSL2 文件系统目录和文件,例如调用 WSL2 的 shell 当前目录,正确。


[1] On Ubuntu 20.04, the CR in the shebang line itself no longer seems to be a problem, but the CRs in the remaining lines still are, so the invocation still fails, even more obscurely, which error messages such as : not foundram Files/nodejs/npm: 3:.

[2] The reason is that the Linux dirname utility is used to determine the shell script's own directory in order to determine npm-cli.js's full path, and this utility by design operates from the perspective of the Linux file-system, which ends up expressing the script's Windows file-system location in this indirect, unsupported manner.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 WSL2 与 Windows 上安装的应用程序交互 的相关文章

随机推荐