346

react-native run-android command terminates by leaving a message in android simulator. The message is as follows:

Unable to load script.Make sure you are either running a Metro server or that your bundle 'index.android.bundle' is packaged correctly for release.

Screenshot

What am I doing wrong?

9
  • can you tried this link stackoverflow.com/questions/32572399/… ?
    – errorau
    Commented Mar 31, 2019 at 14:54
  • 20
    I have the same problem because my device is not connected to the internet.
    – Holi Boom
    Commented May 22, 2019 at 1:44
  • This error comming: throw er; // Unhandled 'error' event
    – Saeed
    Commented May 31, 2019 at 15:36
  • 1
    i found it and put it down stackoverflow.com/a/58570426/6852210
    – Alireza
    Commented Dec 3, 2019 at 6:41
  • 1
    Please make sure your mobile data or Wi-Fi is turned on(this was the problem for me). If so then try the alternatives prescribed here Commented Dec 30, 2020 at 8:10

71 Answers 71

211

You haven't started the bundler yet. Run npm start or react-native start in the root directory of your project before react-native run-android.

15
  • 25
    You have to have 2 node console running simultaneously. One starts and runs the bundler, the other runs the emulator.
    – Nerdragen
    Commented Mar 31, 2019 at 16:02
  • 110
    For me, this error appears even if the bundler is started.
    – P.Lorand
    Commented Apr 23, 2019 at 8:09
  • 6
    Make sure port 8081 isn't blocked. Also, make sure that you use one terminal to start the bundler, wait for it to finish loading the dependency graph, then run react-native run-android on another terminal.
    – Nerdragen
    Commented Apr 23, 2019 at 14:16
  • 1
    @Arbaz.in yes, you need to wait for the dependency graph to finish loading first
    – Nerdragen
    Commented Jul 23, 2019 at 12:53
  • 1
    In windows it runs by itself, but on linux it doesn't, thx! Work perfect! Commented Aug 22, 2019 at 18:03
142

These steps really help me:

Step 1: Create a directory in android/app/src/main/assets

Linux command: mkdir android/app/src/main/assets

Step 2: Rename index.android.js (in root directory) to index.js (Maybe there is an index.js file in which case you do not need to rename it) then run the following command:

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

Step 3: Build your APK: react-native run-android

Please use index.js in latest version.

Enjoy :)

14
  • 15
    This can only run the app for the moment but how will this enable the debugging and hot loading? this is not the perfect solution to getting this done.
    – buddhiv
    Commented Feb 21, 2020 at 6:09
  • 2
    @buddhiv Then what is the perfect solution?
    – Marfin. F
    Commented May 5, 2020 at 22:19
  • 2
    It worked fine for me. But I couldn't understand what happened. This error started to happen just because.
    – JmLavoier
    Commented Jul 24, 2020 at 3:03
  • 2
    It worked and I can debug and the hot loading works as well although as @JmLavoier said I don't know why it stopped working and why now it is working again
    – kingston
    Commented Dec 5, 2020 at 15:48
  • 2
    Now logging not works, how to start it again Commented Dec 21, 2020 at 11:47
123

If you have everything correctly configured then try this:

adb reverse tcp:8081 tcp:8081

Why?

When the RN packager is running, there is an active web server accessible in your browser at 127.0.0.1:8081. It's from this server that the JS bundle for your application is served and refreshed as you make changes. Without the reverse proxy, your phone wouldn't be able to connect to that address.

All credits to Swingline0 .

2
  • Could you please add to your answer why this should work and what this command does?
    – creyD
    Commented Jun 24, 2019 at 8:22
  • I was having OP's problem when debugging by USB, this solved it. Commented Dec 6, 2023 at 18:20
57

A similar issue happened with me.
Apparently Mcafee blocks 8081 port. Took me hours to figure this out.

Try running:

react-native run-android --port=1234

When app shows up with an error on emulator, get into dev settings (Ctrl+M).

Change the "Debug server host and port for device" to "localhost:1234".

Close the app and start it from the app drawer.

6
  • 2
    thanks for a hint, I had to open port 8081 in my firewall and it started working
    – ukie
    Commented Jun 21, 2019 at 18:09
  • I tried to hit the localhost:8081 in browser but no luck. Little research and found out that macafee has blocked that port. Folllowed your way. Started working perfectly fine. Commented Jun 7, 2020 at 11:36
  • I lolled about the mcaffee part, but it also worked for me. Really appreaciated.:)
    – Bart
    Commented Jun 8, 2020 at 17:26
  • This Hint was what I need (Windows 10) basically port 8081 was blocked, and once I killed all that was running on that port, my App magically started working. A usefully command was the following , which killed all processes on port 8081: for /f "tokens=5" %a in ('netstat -aon ^| find ":8081" ^| find "LISTENING"') do taskkill /f /pid %a
    – Ebe
    Commented Nov 30, 2020 at 14:22
  • This is what exactly seems to be happening on my system when I started with 1234 everything works fine, the exception was misleading for a while, going crazy, don't know how my day would have ended if not I land here, thank you!
    – Naga
    Commented Feb 11, 2022 at 17:25
52

For me this error was caused by an upgrade of react-native

Starting with Android 9.0 (API level 28), cleartext support is disabled by default.

If you check out the upgrade diff you need to create a debug manifest android/app/src/debug/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning" />
</manifest>

See for more info: https://stackoverflow.com/a/50834600/1713216

https://react-native-community.github.io/upgrade-helper/

5
  • 2
    Worked for me on RN 0.61.3.
    – Żabojad
    Commented Oct 30, 2019 at 16:54
  • 5
    Unfortunately, this did not work for me. I already had this and am still getting the same error, even though the Metro Bundler is running.
    – Andrew
    Commented Oct 31, 2019 at 19:18
  • I've found I sometimes need extra overrides if I have a networkSecurityConfig defined, it seems to always be these settings causing the issues for me though. You can try removing any security config, and specify android:usesCleartextTraffic="true" in the main AndroidManifest.xml see if that works?
    – Tom
    Commented Nov 4, 2019 at 20:38
  • 1
    I have all of that settings including android:usesCleartextTraffic="true", but I still can't run app on real device.
    – Lucky_girl
    Commented Jul 16, 2020 at 12:11
  • Worked for me. To add some clarity, this was specifically a step mentioned in the RN upgrade docs: reactnative.dev/docs/… I can recommend just copying the debug AndroidManifest from the RN template project, worked a charm: github.com/facebook/react-native/blob/master/template/android/… Commented Dec 10, 2020 at 5:02
34

You can try the following:

Add this line on your AndroidManifest.xml

<application
[...]
android:usesCleartextTraffic="true"
/>
[...]
</application>

EDIT: Be careful, it must be false on production for the security of your app

0
20

just add three splash in : node_modules\metro-config\src\defaults\blacklist.js

replace this part:

var sharedBlacklist = [
  /node_modules[\/\\]react[\/\\]dist[\/\\].*/,
  /website\/node_modules\/.*/,
  /heapCapture\/bundle\.js/,
  /.*\/__tests__\/.*/
];
0
14

First do steps 4 and 5 then you can run your project. If you do not get the result (with steps 4 and 5) do the following steps

1- Try to downgrade your Node version (current version is 12.13.1)

  choco uninstall nodejs
  choco install nodejs --version 12.8

2- Add the path of npm module (C:\Users\your user name\AppData\Roaming\npm) to system variables instead of user variables

3- Install react native globally by using command

  npm install -g react-native-cli

4- Go to the root of your project directory and run the following command :

  react-native start

5- Open another terminal in the root of your project and run the following command :

   react-native run-android 

EDIT :

You are using Genymotion ? If yes, do the following step :

After above step if you get the following error ?

error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.

Open your genymotion and go to :

genymotion menu -> Settings -> ADB -> Then select use custom android sdk tools (click on browse to find sdk location)

Finally , run you project again ..

3
  • 1
    12.9 also worked for me, Thanks, I had upgraded to version 12.13.x and started getting the above problem. Commented Dec 3, 2019 at 11:59
  • Background to this answer: github.com/facebook/react-native/issues/26598 Commented Jan 3, 2020 at 18:53
  • 1
    This worked for me, but then I got development server returned response error code: 500... Metro has encountered an error: EACCESS: permission denied /tmp/metro-cache/33/.... A quick chmod +777 /tmp -R on my local computer solved the problem. Warning: never +777 :-D
    – user1300214
    Commented Aug 31, 2020 at 8:24
13

In my case, the emulator's Wifi and Mobile Data was off.

1
  • this works for me. who would have thought that turning the mobile data off won't crash the app. thank you!
    – kohane15
    Commented Jul 27, 2023 at 7:40
11

I've encountered the same issue while following the React Native tutorial (developing on Linux and targeting Android).

This issue helped me resolve the problem in following steps. Run following commands in the below sequence:

  1. (in project directory) mkdir android/app/src/main/assets
  2. react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
  3. react-native run-android

You can automate the above steps by placing them in scripts part of package.json like this:

"android-linux": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && react-native run-android"

Then you can just execute npm run android-linux from your command line every time.

1
  • Well sonofab*tch! This (new) project I'm on had that EXACT script (step 2) in the package.json but I had never needed something like it before, so I ignored it. That was a tough lesson for me to learn.
    – Mike S.
    Commented Apr 14, 2022 at 16:39
10

[Quick Answer]

After try to solve this problem in my workspace I found a solution.

This error is because there are a problem with Metro using some combinations of NPM and Node version.

You have 2 alternatives:

  • Alternative 1: Try to update or downgrade npm and node version.
  • Alternative 2: Go to this file: \node_modules\metro-config\src\defaults\blacklist.js and change this code:

    var sharedBlacklist = [
      /node_modules[/\\]react[/\\]dist[/\\].*/,
      /website\/node_modules\/.*/,
      /heapCapture\/bundle\.js/,
      /.*\/__tests__\/.*/
    ];
    

    and change to this:

    var sharedBlacklist = [
      /node_modules[\/\\]react[\/\\]dist[\/\\].*/,
      /website\/node_modules\/.*/,
      /heapCapture\/bundle\.js/,
      /.*\/__tests__\/.*/
    ];
    

    Please note that if you run an npm install or a yarn install you need to change the code again.

0
10

In my case I was trying to run the application on the emulator. But, I was getting this

enter image description here

This IP 10.0.2.2 was accessible from emulator chrome browser. The issue is that this IP is not whitelisted in Android Network Security Settings. So, whatever IP Address you are seeing here add that in below settings and you are good to go.

./android/app/src/main/AndroidManifest.xml

        <application
                android:name=".MainApplication"
+               android:usesCleartextTraffic="true"   <- Add this line
                android:allowBackup="true"
                android:label="@string/app_name"
                android:icon="@mipmap/ic_launcher"

./android/app/src/main/res/xml/network_security_config.xml
</network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">10.0.1.1</domain>
        <domain includeSubdomains="true">10.0.2.2</domain>
        <domain includeSubdomains="true">10.0.3.2</domain>
    </domain-config>
 </network-security-config>

Just replace <domain includeSubdomains="true">10.0.2.2</domain> with the IP you are shown in the error from react-native.

1
  • Can you mention exactly where to add these lines in manifest.xml file? And "</network-security-config>" adding this tag provides an error. Or "<domain-config cleartextTrafficPermitted="true">" only adding these tags would be okay? Commented Feb 23 at 7:06
9

This worked for me after trying several ways.

In the file node_modules\metro-config\src\defaults\blacklist.js

Replace :

var sharedBlacklist = [
  /node_modules[/\\]react[/\\]dist[/\\].*/,
  /website\/node_modules\/.*/,
  /heapCapture\/bundle\.js/,
  /.*\/__tests__\/.*/
];

with :

var sharedBlacklist = [
  /node_modules[\/\\]react[\/\\]dist[\/\\].*/,
  /website\/node_modules\/.*/,
  /heapCapture\/bundle\.js/,
  /.*\/__tests__\/.*/
];

hope this helps.

1
  • 2
    From the looks of it, metro-config used \ for paths instead of /, which this answer suggests to modify. Do not do this. Modifying npm packages means your whole team has to do this on their machine, in CI, in staging, in production, and every time metro-config releases an update. A better approach is to fork metro-config on GitHub, commit a fix, push and submit a PR to fix it for everyone. Commented Jan 5, 2020 at 4:28
8

Starting with Android 9.0 (API level 28), cleartext support is disabled by default.

This is what you need to do to get rid of this problem if you do normal run commands properly

  1. npm install
  2. react-native start
  3. react-native run-android

And modify your android manifest file like this.

<application
    android:name=".MainApplication"
    android:icon="@mipmap/ic_launcher"
    android:usesCleartextTraffic="true" // add this line with TRUE Value.
android:theme="@style/AppTheme">
0
7

If you are on linux open the terminal from the App root directory and run

npm start

then open another terminal window and run:

react-native run-android
0
7

A combination of 2 answers solved my problem. It was port related.

adb reverse tcp:8088 tcp:8088
react-native run-android --port=8088

By doing this, the app loaded fine of my phone connected by USB. I think my AV or Vagrant or something else in my PC was using that port.

You can change 8088 to something else if needed.

0
6

Like most of us I assume you are running on VSCODE. In my case, I ran

npx react-native start

from a seperate terminal

Now run npx react-native run-android from your terminal in VSCODE

6

Working solution Updated 12/25/2023

Step 1. Assuming that you're in the root directory of your project, create a directory:

mkdir android/app/src/main/assets

Step 2. Run the following command:

npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

Step 3. Verify that the directory and its files were created

Step 4. npm start

Step 5. Select the appropriate option (i, a, d, r)

Step 6. Open a new terminal and run: npm run android

0
4

My solution to this is as below:

Start Metro server

$ react-native start

Start Android

$ react-native run-android

If see errors saying "port 8081 already in use", then you can kill that process and rerun

$ react-native start

See React Native Troubleshooting Page .

0
4

After hours searching for a answer. The solution was to make a downgrade node to version 12.4.

In my case I realize that the error just occurs in version react native 0.60 with node version 12.6.

4

I'm sure it some combination of the other things mentioned here like allowing clear text, which I enabled for localhost. But this was the final piece in the puzzle.

project.ext.react = [
    entryFile: "index.js",

    // ADD THESE THREE

    bundleAssetName: "index.android.bundle",
    bundleInDebug: true,
    bundleInRelease: true
]

RN: 0.61.3

2
  • this is what fixed it for me! I was building a variant, and needed bundleInStaging: true for the staging variant! Too bad I didn't see this answer until way later, this could have really helped me same some time!
    – mswieboda
    Commented Dec 9, 2021 at 14:58
  • could be nicer if you mentioned where this is supposed to be ie : app/build.gradle Commented Apr 1, 2022 at 18:34
3

I just want to add a non-obvious possibility not covered here. I am using @react-native-community/netinfo for detecting network changes, primarily network state. To test network-off state, the WIFI switch (on the emulator) needs to be switched off. This also effectively cuts off the bridge between the emulator and the debug environment. I had not re-enabled WIFI after my tests since i was away from the computer and promptly forgot about it when i got back.

There is a possibility that this could be the case for somebody else as well and worth checking before taking any other drastic steps.

0
3

Apparently, none of the answers fixed the issue for me. I was able to run the react-native app on emulator but the same code (even when the Server was running), gave me this error when running the app on the device.

I was able to resolve it by executing the following command in the terminal:

adb reverse tcp:8081 tcp:8081

3

I have also faced this issue. I resolved this by following step.

Check android sdk path in Environment Variable.

Add ANDROID_HOME = C:\Users\user_name\AppData\Local\Android\Sdk in System Variable
and C:\Users\user_name\AppData\Local\Android\Sdk\platform-tools path in System Variable

replace sharedBlacklist as below code segment,

var sharedBlacklist = [
  /node_modules[\/\\]react[\/\\]dist[\/\\].*/,
  /website\/node_modules\/.*/,
  /heapCapture\/bundle\.js/,
  /.*\/__tests__\/.*/
];

in node_modules/metro-config/src/default/blacklist.js

Then run npx react-native run-android --port 9001

2

I was having the same trouble, the problem for me was that adb was not in the right environment path, the error is telling you metro port, while you're in the adb, ports are killed and restarted.

Add Enviroment Variable (ADB)

  1. Open environment variables
  2. Select from the second frame PATH variable and click edit option below
  3. Click on add option
  4. Submit the sdk platform tools path C:\Users\ My User \AppData\Local\Android\Sdk\platform-tools

Note: Or depending where is located adb.exe in your machine

  1. Save changes

Run android build again

$ react-native run-android

Or

$ react-native start

$ react-native run-android
2

In my case, I've set a proxy in my emulator. It works back to normal after I removing that proxy.

2

possibility of this error is also the wrong path,check once

 export ANDROID_HOME=/Users/microrentindia/Library/Android/sdk/
 export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
0
2

update this part in metro blacklist

var sharedBlacklist = [
  /node_modules[\/\\]react[\/\\]dist[\/\\].*/,
  /website\/node_modules\/.*/,
  /heapCapture\/bundle\.js/,
  /.*\/__tests__\/.*/
];
2

Try These steps if you have tried everything mentioned in above solutions:

  1. Create File in android/app/src/main/assets
  2. Run the following command :

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

  1. Now run your command to build for e.g. react-native run-android
1
  • Wow this helped me partialy. I was preparing my app for google play market all i did is add new assets icons for android and change target versin to 28. I had to change back to v27 version and all start working again... I dont know why react not working with sdk v28. Commented Apr 28, 2020 at 18:59
2

I have run into similar issue. npx react-native init creates .gitignore file which ignores <project>/android/app/src/debug folder. If you have later cloned this project, this folder would be missing. The solution is simple. In future add this line to the bottom of the .gitignore file.

!android/app/src/debug

For your current project ask the project creator to commit this folder. I have encountered this error with react native version 0.63

Not the answer you're looking for? Browse other questions tagged or ask your own question.