簡體   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