3
votes

I'm doing web development on Windows 10 using Windows Subsystem for Linux with Ubuntu 18.04. Using Node.js and NPM inside Ubuntu and running dev servers and API servers works.

When I am inside WSL I can run commands:

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

And now there is a new situation. I have a project using Nightmare.js (improved but similar to Phantom.js) which will install Electron headless browser when running npm i. Running npm i inside WSL will install Ubuntu version of Electron and when trying to run nothing will happen, browser will not be open. Which is logical because WSL Ubuntu has no visual environment thus can't open a browser in it.

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...

This creates and interesting precedent. It looks like I will need to sometimes run my applications with Windows commands. 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. This is not very convenient and I would like to do it all from WSL. For an example of this behavior there is Visual Studio Code from Microsoft. With Remote-WSL add-on installed I can run code command inside WSL Ubuntu and VSCode will open in Windows.

Here is my question: Is there a way to link commands in WSL Ubuntu to Host Windows system just like it is done with 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.

1

1 Answers

2
votes

There is little to no attention to the issue, so I decided to fix my self. I'm still looking for better/native solution but here is a workaround I came up with.


WSL-Link

Allows WSL users to run any CMD commands on host Windows system from within linux subsystem.

Requirements

  • Windows 10
  • WSL
  • Node.js
  • NPM

Node and NPM have to be installed both in linux subsystem and on Windows host system.

Install

wsl-link has to be installed separately in subsystem and on Windows host

On subsystem linux:

npm i wsl-link -g

On Windows host:

npm i wsl-link -g

Windows Startup

To run script at startup on windows I use PM2 with supplied daemon script.

Install pm2 and pm2-windows-startup on Windows host:

npm i pm2 pm2-windows-startup -g

Install pm2-startup:

pm2-startup install

Run wsl-link pm2 daemon:

wsl-link-pm2

Save pm2 list of processes

pm2 save

You can now confirm that the wsl-link app is running, with:

pm2 status

pm2 status display

Usage

On Windows start server (if not using startup setup with pm2):

wsl-link

Use on subsystem linux (npm -v will be run on host Windows):

wsl-link npm -v

Case

On subsystem linux setup a project (or use existing):

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

app.js:

const Nightmare = require('nightmare');

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

Install Windows version of Nightmare.js and run it on Windows.

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

You should see Electron browser open on your Windows host.

Electron Running