Running Expo in Windows Subsystem for Linux (WSL2) #85
thecodedrift
announced in
Thunked
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
If it runs in Node.js, it can run Expo. Even better, if it runs in Linux, you can take advantage of all the excellent React Native tools that may not be utilizing cross-platform utilities like
cross-env
. Since Windows 10 has support for Linux through a virtualized Windows Subsystem for Linux (WSL2), we can use that to run our Expo applications, build tooling, and more.Getting everything working on WSL2 does have a few gotchas, which is why this guide can help you get up and running quickly and avoid some of the most common pitfalls (spoiler: it’s networking related).
Overview
WSLHostPatcher
Setting up WSL2 for Expo Development
WSL2 gives you access to a fully featured Linux command line inside of Windows. While great cross-platform tools like
env-cmd
exist, not every tool we use in React Native development is Powershell-aware. Using WSL2 eliminates those problems entirely; it’s just Linux. Unless specified, all our commands will be in WSL2’s Linux environment.What You’ll Need for This Step
The first thing we’re going to need is both Expo CLI and EAS CLI in our WSL environment. From there, we can initialize our standard Expo app, just like a managed app. We’re using the Typescript template, but you can also opt for a vanilla JS experience.
We’ll get our Expo QR code, but when we try to visit our developer page, we’ll end up at a 404. This happens because to our browser,
localhost
is referring to Windows, and Windows doesn’t know that those Expo requests need to be forwarded along to the WSL.Configuring
WSLHostPatcher
Unless you have specific security concerns (such as exposing your Windows machine to the Internet), the fastest way to fix the port forwarding problems is with a utility called
[WSLHostPatcher](https://github.com/CzBiX/WSLHostPatcher)
. Built on the Windows Detours framework, it dynamically updates the bind & listen addresses for your WSL instances. We’re going to download the utility and run it every time our WSL instances starts. (Adjust appropriately for a non-BASH environment)Let’s close our dev console, along with any running
yarn
instances, and reload WSL. We’ll be greeted withWSLHostPatcher
confirming it found and updated our WSL instance.Give
expo start
a try now, and watch how yourlocalhost
request automatically finds its way to your Expo instance.Alternative: Windows Firewall Setup
One of the downsides to
WSLHostPatcher
is that it will bind your WSL instance to0.0.0.0
. For most development scenarios, this is perfectly ok. If you have security concerns though, you can also use a Powershell script to forward specific ports to your WSL instance. This is a much longer setup, but uses the built-in Windows Firewall.First, you’ll want to download the following script based on the excellent work of @edwindijas on github, and save it to
c:\scripts\wslbridge.ps1
. This script does a couple things.Open up Task Scheduler and set up a task that will run the script on login, after a small delay. For reference, your task should look like the following:
Creating a task in the Task Scheduler
Setting a Trigger condition for our task
Task Action set to start the
wslbridge.ps1
script with the-ExecutionPolicy Bypass
flag, allowing it to run with the necessary permissionsOnce configured, either on login or by running the script manually, Windows will automatically forward ports to WSL for you. From here, you can now run
expo start
from within WSL, andlocalhost
will resolve as expected.Other Things You Can Do With WSL And Expo
The fun doesn’t stop at just accessing Expo’s developer tools. Because you can forward any port to WSL, that means you can access the Chrome Debugger, use
adb
, integrate Flipper, monitor with Wireshark, the list goes on! You’ll also be running your Expo builds from within Linux, meaning any build tooling will work just as it would on a Linux or Mac workstation.It’s a great time to build React Native apps, and now it’s easier than ever on Windows.
Beta Was this translation helpful? Give feedback.
All reactions