How to Install React Native on Windows and make it use a specific IP Address A small guide explaining how to install React Native on a Windows 10 machine and choose the right IP Address when you have multiple LANs / Network Interface Cards

How to Install React Native on Windows and make it use a specific IP Address

Do you know React Native? You most likely do, since you found this post. In case you don't, it's a great framework that lets you build native mobile apps using only JavaScript: it uses the same design as React, letting you compose a rich mobile UI from declarative components.

Unlike Progressive Web Apps built with client-side framerworks, a React Native app is a real mobile app, not a "mobile web app", an "HTML5 app", or a "hybrid app", as the frameworks uses the same fundamental UI building blocks as regular iOS and Android apps written in Objective-C, Swift and Java. If you're interested in taking a look you can check out the React Native official website and/or the React Native GitHub repository, where you will find a decent Getting Started guide and a link to the Documentation.

In this chapter we'll deal with a specific issue that might affect you when trying to install React Native on Windows machines, which can be normally done in less than 5 minutes but could be troublesome if you got multiple IP Addresses on your development machine - which is pretty common, since most of us will have multiple NICs, VMWare virtual adapters and so on.

Installing React Native on Windows

Let's start with the basics: React Native is basically a (huge) NPM package, therefore the first thing to do is to install Node.js.

Installing Node.js

Installing Node.js in a Windows environment is rather easy to do, as long as you follow the following suggestions to avoid the most common issues:

  • Get the Node.js LTS version for Windows 64-bit (if you can, otherwise the 32-bit), ignoring the Latest Version.
  • Uninstall any previous Node.js and/or NPM version (including those shipped with development frameworks such as Visual Studio) you.
  • Do NOT install it in C:\Program Files\ or C:\Program Files (x86)\ to avoid critical permissions issues: just use C:\nodejs\ or a similar "permission free" new folder.

If you follow these guidelines, you shouldn't run into troubles.

Upgrading NPM

Upgrading npm on Windows requires manual steps to ensure that PowerShell/CMD find the new version of npm. Therefore, the best way to update npm on Windows is to use the awesome npm-windows-upgrade package,  a small tool that will reduce such process to the following few steps:

  • Run PowerShell as Administrator using Start > search for PowerShell > Right-click > Run as Administrator.
  • Execute the following PowerShell command: Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
  • Install the npm-windows-upgrade package by typing npm install --global --production npm-windows-upgrade
  • Once done, launch it with the following command: npm-windows-upgrade

The tool will show you a list of all the published and available versions of npm (including pre-release and beta versions): choose the one you want to install - arguably the most recent one - and the tool will do the rest.

Installing the CreateReactNativeApp package

The easiest way to start building a new React Native application on Windows is by installing and launching the Create React Native App npm package: such package allows you to start a project without installing or configuring any tools or frameworks to build native code (!).

To install it, simply type the following command from an elevated PowerShell prompt:

Then run the following commands to create, initialize and launch a new React Native project called "MyTestProject":

The first of the commands above will create a "MyTestProject" subfolder in the folder you are in: be sure to change the path of your PowerShell prompt accordingly before launching it.

Running the App

The above commands will launch a local development server that contains the app, such as in the following screenshot:

How to Install React Native on Windows and make it use a specific IP Address

As we can see, it's pretty awesome: we can even access the app from our Smartphone by installing the Expo Client app (available from Android from Google Play or for iOS through the App Store) and scan a QR Code... as long as our phone is on the same local network as our "server" machine.

Seems pretty easy, right? Well, unfortunately there are at least two things that you will need to do in order to allow this awesome connection method on a Windows machine:

  • Create a incoming connection rule within the Windows Firewall for the port shown (in the above screenshot is the TCP port 19000).
  • Ensure that the create-react-native-app will pick the proper IP address among all those installed on your machine.

The former issue is rather trivial - we will just assume that the average reader can deal with it: conversely, the latter requires some hidden knowledge.

The "Wrong IP" issue

The issue is a consequence of the fact that the create-react-native-app package is using the npm's ip package to auto-detect the server's IP address, which - on Windows machine - picks the IP address used by the NIC having the highest interface metric (aka Adapter Priority Setting) configured: in other words, it will always pick the first IP address shown when running ipconfig from a Windows command prompt.

For additional info on how to set the Adapter Priority Settings for your Network Interface Cards on Windows 10 you can take a look at this post. However, you don't need to do that: luckily enough, there's a simpler solution to this nasty issue.

The Fix

To ensure that the create-react-native-app package will pick a specific IP Address, follow these steps:

  • Open Start > Control Panel > System
  • Go to Advanced System Settings > Environment Variables 
  • Set a System Environment Variable with the following name/value pair:
    • name: REACT_NATIVE_PACKAGER_HOSTNAME
    • value: <the IPv4 address you want to use>

That's about it!

IMPORTANT: remember to close and re-open the elevated PowerShell command prompt, since newly added Environment Variables wont' be available on the existing prompts.

If you're looking for an alternate method to install a React Native app on Windows using the react-native init command from the React Native CLI, check out this post; if you want to upgrade this sample "Hello World" app with push notifications support using Firebase, check out this 5-part tutorial. For additional resources about React Native, click here.

 

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.