我无法在 Windows 10 上使用 WSL2 ON 的 npm 命令(ubuntu 终端) [英] I can't use the npm command on Windows 10 with WSL2 ON (ubuntu terminal)

查看:45
本文介绍了我无法在 Windows 10 上使用 WSL2 ON 的 npm 命令(ubuntu 终端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 npm 命令工作,但它不想.正如我在标题中所说,我使用的是 Windows WSL2.很久以前我已经用 Git Bash 安装了 nodejs,但现在我需要它在我的 ubuntu 终端上工作.我该如何解决这个问题?

我的 ubuntu 终端:

<块引用>

root@DESKTOP-U2RC2DU:~# node -vv10.19.0root@DESKTOP-U2RC2DU:~# npm-bash:/mnt/c/Program Files/nodejs/npm:/bin/sh^M: 错误的解释器: 没有那个文件或目录root@DESKTOP-U2RC2DU:~#

解决方案

检查您在 WSL 中的路径.如果在 Linux 版本之前找到 Windows 版本,则可能会导致问题.

我知道有一些可能的解决方法:

  1. 创建一个启动脚本(例如 bashrc 或您选择的 shell 的等效文件),从路径中删除 Windows 节点,或者至少为 Linux 版本提供更高的优先级.可能最简单的解决方案是在路径前添加任何 Linux 版本位置.

  2. 创建一个 /etc/wsl.conf 与 ...

    [互操作]appendWindowsPath = false

    这将从 WSL 会话中删除所有 Windows 路径.缺点是一些有用的 Windows 实用程序将不再位于 WSL 路径中,但您始终可以在启动脚本中手动重新添加它们.从脚本角度来看,这可能比手动删除路径更容易,至少.

  3. 如果您不再需要它,并且将专门使用 Linux/WSL 版本,您可以卸载 Windows nodejs.

I'm trying to get the npm command to work but it doesn't want to. As i said in the title, I'm using the windows WSL2. I already have installed nodejs with Git Bash a long time ago but now I need it to work on my ubuntu terminal. How could I fix this issue?

My ubuntu terminal:

root@DESKTOP-U2RC2DU:~# node -v
v10.19.0
root@DESKTOP-U2RC2DU:~# npm
-bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
root@DESKTOP-U2RC2DU:~#

解决方案

Check your path in WSL. If the Windows version is found before the Linux version, it can cause problems.

There are a few possible workarounds for this that I know of:

  1. Create a startup script (e.g. bashrc or equivalent for your shell of choice) which removes the Windows node from path, or at least gives the Linux version higher precedence. Probably the simplest solution is to prepend any Linux version location to the path.

  2. Create a /etc/wsl.conf with ...

    [interop]
    appendWindowsPath = false
    

    This will remove all Windows paths from the WSL session. The downside is that some useful Windows utilities will no longer be in the WSL path, but you can always add them back in manually in your startup scripts. This is probably easier from a scripting perspective than removing paths manually, at least.

  3. If you don't need it anymore, and will be using the Linux/WSL version exclusively, you could uninstall the Windows nodejs.

这篇关于我无法在 Windows 10 上使用 WSL2 ON 的 npm 命令(ubuntu 终端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆