简体   繁体   English

适用于 Linux (WSL) 的 Windows 子系统使用与 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

I have recently moved to a Windows + WSL environment (WSL is going very good by the way).我最近搬到了 Windows + WSL 环境(顺便说一下,WSL 进展顺利)。 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).这样做的主要原因是有一个用于开发的 Linux 环境,并有一个用于其他应用程序和游戏的 Windows,而无需重新启动我的计算机(之前有一个双启动设置)。

In the setup process, I found most of the Windows installed binaries can be executed from WSL.在设置过程中,我发现大多数 Windows 安装的二进制文件都可以从 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).因此,我可以在 Windows 中安装 java jdk 并将二进制文件符号链接到 WSL共享 jdk 安装,这完美无缺)。 But doing the same with node, happens that node npm and npx binaries are not working :(但是对节点做同样的事情,节点 npm 和 npx 二进制文件不工作:(

I wannted to have a single node installation which I could manage using using nvm windows .我想拥有一个可以使用nvm windows进行管理的单节点安装。 So I started the installation the following way:所以我通过以下方式开始安装:

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/ :在 WSL 中,我按照此处的 Nick Janetakis 指南(感谢Nick )配置了我的/etc/wsl.conf ,以便将 Windows 驱动器安装在/而不是/mnt/

/etc/wsl.conf /etc/wsl.conf

[automount]
root = /
options = "metadata"

Then installed node in windows:然后在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

Everything working as expected so far.到目前为止,一切都按预期工作。 The next step is to symlink the windows node binaries to WSL.下一步是将 Windows 节点二进制文件符号链接到 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

So inside WSL terminal (remember that my disks are mounted at /c not /mnt/c as the default behaviour):所以在 WSL 终端内(请记住,我的磁盘安装在 /c 而不是 /mnt/c 作为默认行为):

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

And...和...

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

Great!伟大的! ( Note: as node is installed on windows, when being on WSL you must use it inside a disk drive, /d in this case). 注意:由于节点安装在 Windows 上,当在 WSL 上时,您必须在磁盘驱动器中使用它,在这种情况下是 /d)。 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)

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 试图在一个路径中找到 npm-cli.js,该路径是 Windows 路径中 npm 符号链接位置的有线组合。

Is there a way to tell npm/npx the correct Windows path where it must find its files from WSL?有没有办法告诉 npm/npx 它必须从 WSL 中找到其文件的正确 Windows 路径?

Sorry for the long question but due to the very particular setup I considered that contextualization necessary.很抱歉这个冗长的问题,但由于非常特殊的设置,我认为上下文化是必要的。

Any workaround?任何解决方法? I have met the same situation where I hope to share the same node and npm between WSL and Windows, as I want to run them in a terminal (WSL) and in IDEA (Windows) at the same time.我遇到了同样的情况,我希望在 WSL 和 Windows 之间共享相同的节点和 npm,因为我想同时在终端(WSL)和 IDEA(Windows)中运行它们。

I found that npm cannot be run through drviers, like using npm under C:/ , which has been installed under F:/ , resulting in an error:我发现npm不能通过drviers运行,比如在C:/下使用npm,已经安装在F:/ ,导致报错:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'C:\f\Users\aleen\AppData\Roaming\nvm\v10.21.0\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

However, we always work in one driver, which means we can install npm under the common used driver ( F:/ for me), and make an alias to run it inside the driver via relative path:但是,我们总是在一个驱动程序中工作,这意味着我们可以在常用驱动程序(对我来说是F:/ )下安装npm,并通过相对路径在驱动程序中创建一个别名来运行它:

# ~/.bash_aliases
function npm() {
  $(realpath --relative-to="$(pwd)" /mnt/f/Program\ Files/nodejs)/npm $@
}
export -f npm

I could not install npm in WSL using Ubuntu 20.04 LTS in Windows 10.我无法在 Windows 10 中使用 Ubuntu 20.04 LTS 在 WSL 中安装 npm。

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!).请注意,它说它适用于 WSL2,但安装节点的步骤在 WSL1 环境中是有效的(现在是 7 月 20 日,我仍然无法在我的 Windows 10 版本中获得 WSL,啊!)。

In a nutshell this solution gets you to install nvm (node version manager) in your WSL environement.简而言之,此解决方案让您在 WSL 环境中安装 nvm(节点版本管理器)。

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

You can then install a newer version of node which ships with npm by default eg然后,您可以安装默认情况下随 npm 一起提供的较新版本的节点,例如

nvm install --lts

I have my own development environment, so I couldn't test it on the same environment of yours.我有我自己的开发环境,所以我无法在你的相同环境中测试它。 But, I suggest that you should check whether npm under "Program Files" works well on WSL.但是,我建议您应该检查“程序文件”下的 npm 是否在 WSL 上运行良好。

user@host:~$ /c/Program\ Files/nodejs/npm -v

In my case, another error occurs when running above command.就我而言,运行上述命令时会发生另一个错误。

Error: EINVAL: invalid argument, uv_pipe_open

If it is the same on your environment, you may solve this issue first.如果您的环境相同,您可以先解决此问题。

And, about module path issue, it seems to be caused by path;而且,关于模块路径问题,它似乎是由路径引起的; the original npm(under Program Files) and your symbolic link have different current path.原始 npm(在 Program Files 下)和您的符号链接具有不同的当前路径。

I modified the original npm as below:我修改了原来的 npm 如下:

#!/bin/sh
(set -o igncr) 2>/dev/null && set -o igncr; # cygwin encoding fix

basedir=`dirname "$0"`

echo $basedir  # Added code

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
...

If you run the original npm and your symbolic link, $basedir will shows different results, and it causes module path issue.如果运行原始 npm 和符号链接, $basedir 将显示不同的结果,并导致模块路径问题。

If you can solve the first issue(uv_pipe_open error), how about adding the nodejs directory on your path instead of symbolic links?如果您可以解决第一个问题(uv_pipe_open 错误),那么在您的路径上添加 nodejs 目录而不是符号链接如何?

what I did is I modified the npm bash file located at C:\\Program Files\\nodejs我所做的是修改了位于C:\\Program Files\\nodejs的 npm bash 文件

replacing替换

"$NODE_EXE" "$NPM_CLI_JS" "$@"

to

"$NODE_EXE" "/Program Files/nodejs/node_modules/npm/bin/npm-cli.js" "$@"

then save it as a unix format file inside wsl然后将其保存为 wsl 中的 unix 格式文件

Consolidating and expanding on existing answers ...巩固和扩展现有答案...

First, the primary answer here is:首先,这里的主要答案是:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM