适用于 Linux 的 Windows 子系统 (WSL) 使用与 Windows 共享的 Node.js 安装:Node.js npm &npx 二进制文件不起作用 [英] Windows Subsystem for Linux (WSL) using shared Node.js installation with Windows: Node.js npm & npx binaries not working

查看:133
本文介绍了适用于 Linux 的 Windows 子系统 (WSL) 使用与 Windows 共享的 Node.js 安装:Node.js npm &npx 二进制文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近搬到了 Windows + WSL 环境(顺便说一下,WSL 进展得非常好).这样做的主要原因是有一个 Linux 环境进行开发,并有 Windows 用于其他应用程序 &无需重新启动计算机即可玩游戏(之前有双启动设置).

I have recently moved to a Windows + WSL environment (WSL is going very good by the way). The main reason of doing this is to have a Linux environment for development and having Windows for other apps & games without having to reboot my computer (had a dual-boot setup before).

在安装过程中,我发现大多数 Windows 安装的二进制文件都可以从 WSL 执行.因此,不是重复安装(例如:在 Windows 中安装 java 和 maven 以使用 Eclipse IDE,然后在 WSL 中单独安装它以在终端中使用它)我可以在 Windows 中安装 java jdk 并将二进制文件符号链接到 WSL共享 jdk 安装,这完美无缺).但是对 node 做同样的事情,发生 node npm 和 npx 二进制文件不起作用:(

In the setup process, I found most of the Windows installed binaries can be executed from WSL. So instead of duplicating installations (eg: installing java and maven in Windows in order to use Eclipse IDE and then installing it in WSL separately to use it in the terminal) I could just install java jdk in Windows and symlink the binaries to WSL in order to share the jdk installation, this worked flawlessly). But doing the same with node, happens that node npm and npx binaries are not working :(

我想要一个可以使用 nvm windows 管理的单节点安装.所以我以如下方式开始安装:

I wannted to have a single node installation which I could manage using using nvm windows. So I started the installation the following way:

在 WSL 中,我按照 Nick Janetakis 指南(感谢 Nicka>) 为了在 / 而不是 /mnt/ 安装 Windows 驱动器:

In WSL, I configured my /etc/wsl.conf, following Nick Janetakis guide here (thanks Nick) in order to mount Windows drives at / instead of /mnt/:

/etc/wsl.conf

/etc/wsl.conf

[automount]
root = /
options = "metadata"

然后在windows中安装节点:

Then installed node in windows:

C:\Windows\system32> nvm install 10.15.0
... installing process...
C:\Windows\system32> nvm use 10.15.0
...success message...
C:\Windows\system32> node -v
v10.15.0
C:\Windows\system32> npm -v
6.4.1

到目前为止一切都按预期工作.下一步是将 windows 节点二进制文件符号链接到 WSL.二进制文件位于:

Everything working as expected so far. The next step is to symlink the windows node binaries to WSL. The binaries are located at:

C:\Windows\system32> where node
C:\Program Files\nodejs\node.exe

C:\Windows\system32> where npm
C:\Program Files\nodejs\npm
C:\Program Files\nodejs\npm.cmd

C:\Windows\system32>where npx
C:\Program Files\nodejs\npx
C:\Program Files\nodejs\npx.cmd

因此在 WSL 终端内部(请记住,我的磁盘安装在/c 而不是/mnt/c 作为默认行为):

So inside WSL terminal (remember that my disks are mounted at /c not /mnt/c as the default behaviour):

user@host:~$ mkdir ~/bin
user@host:~$ ln -s /c/Program\ Files/nodejs/node.exe ~/bin/node
user@host:~$ ln -s /c/Program\ Files/nodejs/npm ~/bin/npm
user@host:~$ ln -s /c/Program\ Files/nodejs/npx ~/bin/npx

还有……

user@host:/d/tmp$ node -v
v10.15.0
user@host:/d/tmp$ echo "console.log('Hello World');" >> index.js
user@host:/d/tmp$ node index.js
Hello World

太好了!(注意:因为 node 安装在 Windows 上,所以在 WSL 上时,您必须在磁盘驱动器中使用它,在这种情况下为/d).但是...

Great! (Note: as node is installed on windows, when being on WSL you must use it inside a disk drive, /d in this case). But...

user@host:~$ npm -v
internal/modules/cjs/loader.js:583
throw err;
^

Error: Cannot find module 'C:\home\user\bin\node_modules\npm\bin\npm-cli.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

这就是我写这篇文章的原因.错误很明显,npm 试图在路径中找到 npm-cli.js,该路径是 Windows 路径中 npm 符号链接位置的有线组合.

Now that's the reason I'm writing this. The error is clear, npm is trying to find npm-cli.js in a path which is a wired mix of the npm symlink location inside a windows path.

有没有办法告诉 npm/npx 它必须从 WSL 找到它的文件的正确 Windows 路径?

Is there a way to tell npm/npx the correct Windows path where it must find its files from WSL?

抱歉问了这么长的问题,但由于非常特殊的设置,我认为需要进行上下文化.

Sorry for the long question but due to the very particular setup I considered that contextualization necessary.

推荐答案

我无法在 Windows 10 中使用 Ubuntu 20.04 LTS 在 WSL 中安装 npm.

I could not install npm in WSL using Ubuntu 20.04 LTS in Windows 10.

但是,当我按照说明进行操作时 这里 我确实设法让它工作.请注意,它说它适用于 WSL2,但安装节点的步骤在 WSL1 环境中是有效的(现在是 7 月 20 日,我仍然无法在我的 Windows 10 版本中获得 WSL,啊!).

However when I followed the instructions here I did manage to get it working. Note that it says it is for WSL2, but the steps for installing node are valid in a WSL1 environment (it's July 20 and I still cannot get WSL in my edition of Windows 10, argh!).

简而言之,此解决方案可让您在 WSL 环境中安装 nvm(节点版本管理器).

In a nutshell this solution gets you to install nvm (node version manager) in your WSL environement.

sudo apt-get install curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

然后,您可以安装默认情况下随 npm 一起提供的较新版本的节点,例如

You can then install a newer version of node which ships with npm by default e.g.

nvm install --lts

这篇关于适用于 Linux 的 Windows 子系统 (WSL) 使用与 Windows 共享的 Node.js 安装:Node.js npm &npx 二进制文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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