繁体   English   中英

在 Linux Z3D945423F8E9496C4204A5D8C65B46 的 Windows 子系统中链接 Windows 命令

[英]Linking Windows commands inside Windows Subsystem for Linux Ubuntu

I'm doing web development on Windows 10 using Windows Subsystem for Linux with Ubuntu 18.04. 在 Ubuntu 中使用 Node.js 和 NPM 并运行开发服务器和 ZDB974238714CA8DE634A7CE1D083A 服务器工作。

当我在 WSL 中时,我可以运行命令:

npm i
node app
npm run serve
# etc...

而现在又出现了新情况。 我有一个使用Nightmare.js的项目(改进但类似于Phantom.js ),它将在运行npm i时安装Electron无头浏览器。 在 WSL 中运行npm i将安装 Ubuntu 版本的 Electron 并且尝试运行时不会发生任何事情,浏览器将不会打开。 这是合乎逻辑的,因为 WSL Ubuntu 没有视觉环境,因此无法在其中打开浏览器。

If I run npm i with PowerShell or CMD (npm that is installed on Windows instead WSL will be used), Windows version of Electron will be installed and I can run it with node app inside PowerShell and it will work as expected, browser opens etc ...

这创造了有趣的先例。 看起来我有时需要使用 Windows 命令运行我的应用程序。 Instead of doing npm i or node app directly in WSL I have to switch to CMD or PowerShell and execute them there, so that windows versions of npm and node are used instead of WSL Ubuntu's. 这不是很方便,我想从 WSL 完成这一切。 Microsoft 的 Visual Studio Code 就是这种行为的一个例子。 安装Remote-WSL插件后,我可以在 WSL Ubuntu 中运行code命令,VSCode 将在 Windows 中打开。

这是我的问题:有没有办法将 WSL Ubuntu 中的命令链接到主机 Windows 系统,就像使用 VSCode 一样? Ideally I would like to have something like this in my WSL Ubuntu: windows-npm i , windows-node app that will run npm and node on the Host Windows System instead of the same commands on WSL Ubuntu.

几乎没有注意到这个问题,所以我决定修复自己。 我仍在寻找更好/原生的解决方案,但这是我想出的解决方法。


WSL-链接

允许 WSL 用户从 linux 子系统内的主机 Windows 系统上运行任何 CMD 命令。

要求

  • Windows 10
  • WSL
  • Node.js
  • NPM

节点和 NPM 必须安装在 linux 子系统和 Windows 主机系统中。

安装

wsl-link 必须单独安装在子系统和 Windows 主机上

在子系统 linux 上:

npm i wsl-link -g

在 Windows 主机上:

npm i wsl-link -g

Windows 启动

要在 windows 上启动时运行脚本,我使用 PM2 和提供的守护程序脚本。

在 Windows 主机上安装 pm2 和 pm2-windows-startup:

npm i pm2 pm2-windows-startup -g

安装 pm2-startup:

pm2-startup install

运行 wsl-link pm2 守护进程:

wsl-link-pm2

保存 pm2 进程列表

pm2 save

您现在可以通过以下方式确认 wsl-link 应用程序正在运行:

pm2 status

pm2状态显示

用法

在 Windows 启动服务器上(如果不使用 pm2 启动设置):

wsl-link

在子系统 linux 上使用(npm -v 将在主机 Windows 上运行):

wsl-link npm -v

案子

在子系统 linux 上设置项目(或使用现有项目):

mkdir wsl-link-test
cd wsl-link-test
touch app.js
npm init

应用程序.js:

const Nightmare = require('nightmare');

(async () => {
  await Nightmare({
    show: true
  })
  .goto('https://google.com');
})();

安装 Windows 版本的 Nightmare.js 并在 Windows 上运行它。

wsl-link npm i nightmare --save
wsl-link node app

您应该会在 Windows 主机上看到 Electron 浏览器打开。

电子运行

暂无
暂无

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

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