Unable to load script from assets index.android.bundle on windows
I'm trying to run my first React Native project for first time on my device (Android 4.2.2).
And I get:
unable to load script from assets index.android.bundle
Commands that I used:
cd (project directory)
react-native start
react-native run-android
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.
- (in project directory)
mkdir android/app/src/main/assets
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
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.
If You are running your application on physical device and getting this error
unable to load script from assets index.android.bundle
try running the command:
adb reverse tcp:8081 tcp:8081
It workd for Me...
Using npm version 4.3.0 react-native-cli version 2.01 react-native version 0.49.5
In the project directory,
mkdir android/app/src/main/assets
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
The file name has changed from index.android.js to index.js
I have faced the same problem with a real android device.
Solution: Based on this answer by Niltoid
- Find your local IP
- Open app and shake your Android device
- Go Dev Setting and>Debug Server...
- Paste your IP and port; X.X.X.X:8088" (where X's are your local IP)
for Mac user: open your network preference, here you will get your local IP.
If you are using Windows run the commands in the following way, or if you get an error "Cannot find entry file index.android.js"
mkdir android\app\src\main\assets
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
You can follow the instruction mentioned on the official page to fix this issue. This issue occur on real device because the JS bundle is located on your development system and the app inside your real device is not aware of it's location.
Had the same issue in mac, After spending 2 days i was finally able to get is working.
Since Emulator cellular data was turned off
, I was getting the unable to load script from assets index.android.bundle make sure your bundle is running
.
Make sure your Emulator cellular data is turned on
by doing this package server was able to bundle index.android.js
. Hope it helps for someone who is in development phase.
OS: Windows
Another way to resolve this problem after trying two methods above
(because I installed SDK not in DEFAULT LOCATION like C:\users\"user_name"\appdata\local\sdk)
If you noticed that command line announced: "'adb' is not recognized as an internal or external command..."
So this is how I get over:
Examples: I installed SDK in D:\Android\sdk
So I will insert path in System Variables 2 more lines:
D:\Android\sdk\tools
D:\Android\sdk\platform-tools
(If you don't know how to edit path System Variables, here's the topic: https://android.stackexchange.com/questions/38321/what-do-i-type-in-path-variable-for-adb-server-to-start-from-cmd/38324)
Then I run cmd again: react-native run-android
It worked for me.
I've had this same issue for a number of months now. I initially used @Jerry's solution however, this was a false resolution as it didn't fully fix the problem. Instead, what it did was take every build as a production build meaning that any slight change you made in the app would mean that rebuilding the entire app is necessary.
While this is a temporal solution, it works horribly in the long term as you are no longer able to avail of React Native's amazing development tools such as hot reloading etc.
A proper solution is a shown:
In your MainApplication.java
file, replace the following:
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
with
@Override
public boolean getUseDeveloperSupport() {
return true;
}
as for some reason BuildConfig.DEBUG
always returned false and resulted in a bundle file in the assets directory.
By manually setting this to true, you're forcing the app to use the packager. This WON't work in production so be sure to change it to false or the default value.
Don't forget also to run
$ adb reverse tcp:8081 tcp:8081
Make sure that you have added /path/to/sdk/platform-tools to your path variable.
When you run react-native run-android, it runs adb reverse tcp:< device-port > tcp:< local-port > command to forward the request from your device to the server running locally on your computer. You will see something like this if adb is not found.
/bin/sh: 1: adb: not found
Starting the app (.../platform-tools/adb shell am start -n
com.first_app/com.first_app.MainActivity...
Starting: Intent { cmp=com.first_app/.MainActivity }
I had the same issue even when running on a simulator. I did a quick workaround so that I could have the normal dev workflow.
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
// return true here to load JS from the packager
// BuildConfig.DEBUG for me was set to false which meant it
// it was always trying to load the assets from assets folder.
return true;
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
Probably will have to revert the change when trying to do a production build.
It may be caused by unlinked assets in your React Native project code base, like when you renames your project application/bundle id or adds an external package without link it properly, for example.
Simply try it in your project root directory:
react-native link
react-native run-android
For IOS:
In a terminal:
cd ios
Remove the build folder with: rm -r build
Run again: react-native run-ios
Alternatively, you could open Finder, navigate to YOUR_PROJECT/ios and delete the build folder.
Then run again: react-native run-ios
For ANDROID:
In a terminal:
cd android/app
Remove the build folder with: rm -r build
Run again: react-native run-android
Alternatively, you could open Finder, navigate to YOUR_PROJECT/android/app and delete the build folder.
Then run again: react-native run-android
Ubuntu
first time, I created new app with react-native init project-name
.
I got the same error.
so i do the following steps to resolve this in my case.
- Firstly run
sudo chown user-name-of-pc /dev/kvm
in my case. While debugging from your Android phone, select
Use USB to Transfer photos (PTP)
.Create Folder
assets
in project-name/android/app/src/main- make sure
index.js
be avaiable into your projectroot
directory and then run below command from console aftercd project-name
directory.
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
or for index.android.js then
react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
run command
./studio.sh
inandroid-studio/bin
directory. It will opens up Android Studio.run command
react-native run-android
.
If when running react-native run-android
command the second line of the trace is
JS server not recognized, continuing with build...
it means packager for the app cannot be started and the app will fail to load without some extra steps. Note that the end of the trace still reports success:
BUILD SUCCESSFUL
The problem is probably some port conflict (in my case it was the default IIS site that I completely forgot about). Another manifestation of the problem would be a failure to open http://localhost:8081/debugger-ui
URL in Chrome.
Once the port conflict is resolved the trace will report
Starting JS server...
then an additional Node window will open (node ...cli.js start
) and the app will load/reload successfully.
After that you should be able to open debug console in Chrome with http://localhost:8081/debugger-ui
.
For this problem I just solve it by running:
react-native start
then
adb reverse tcp:8081 tcp:8081
on command line. And then i can run:
react-native run-android
So i will not waste my time to run:
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
every time.
I had issue with McAfee on my mac blocking port 8081, had to change it to 8082.
First run your package server:
react-native start --port 8082
Open another terminal, start the android app as usual:
react-native run-android
Once it finishes, now rewrite the tcp port that adb tunnels:
adb reverse tcp:8081 tcp:8082
See the list of adb tcp tunnels:
adb reverse --list
You should now see a heartwarming message:
(reverse) tcp:8081 tcp:8082
Go to your app and reload, done!
PS: Don't change anything in the app Dev settings, if you added "localhost:8082", just remove it, leave it blank.
EDIT: To all the McAfee victims out there, there's easier solution if you have root access, just temporarily kill the McAfee process sitting on port 8081 and no need for port change at all:
sudo launchctl remove com.mcafee.agent.macmn
I think you don't have yarn installed try installing it with chocolatey or something. It should be installed before creating your project (react-native init command).
No need of creating assets directory.
Reply if it doesn't work.
Edit: In the recent version of react-native they have fixed it. If you want complete freedom from this just uninstall node (For complete uninstallation Completely remove node refer this link) and reinstall node, react-native-cli then create your new project.
I was also facing this problem because when i run projects on emulator its working fine but on real device it gives this error.SO i resolve this problem by the following solution
1st step:First of all open cmd and go to your sdk manager Platform-tools folder i.e.cd C:Development\Android\Sdk\Platform-tools
2nd step:now run this command : adb devices
after this command check your device listed in command propmt
3rd step:now run this adb reverse tcp:8081 tcp:8081
Now your setup is done
4th step:GO to your project directory and run this command
react-native run-android
when I update react-native to 0.49.5, this problem appears and when I followed this Breaking changes and deprecations
it disappeared.
I am facing this issue, lot of search and I resolved that issue.
c:\ProjectFolder\ProjectName> 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
Than Run the react native :
c:\ProjectFolder\ProjectName>react-native run-android
don't forget turn on internet in emulator device, I resovled this error, it work perfect :V I get this error because i turn off internet to test NetInfo :D
For all of you guys who are developing from a create-react-native-app and ejected, and have this issue in development
,
this is what worked for me
just a little detail from the accepted answer
(in project directory) mkdir android/app/src/main/assets
here comes the part that changes cos you are in development get rid of the --dev: false
part:
react-native bundle --platform android --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
after that close all terminals, erase the build folder from android/app/ to make sure you start clean to build your app, open a new terminal, and
(in project directory) npm run android
will prompt the packager console (in another terminal) and build successfully
1 Go to your project directory and check if this folder exists android/app/src/main/assets
- If it exists then delete two files viz
index.android.bundle
andindex.android.bundle.meta
- If the folder assets don't exist then create the assets directory there.
2.From your root project directory do
cd android && ./gradlew clean
3.Finally, navigate back to the root directory and check if there is one single entry file calledindex.js
If there is only one file i.e. index.js then run 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
If there are two files i.e index.android.js and index.ios.js then run this
react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
- Now run
react-native run-android
- Now run
This issue can also be caused by the cellular data of the device / emulator being turned off. Make sure you check that.
for this error :"unable to load script from assets 'index.android.bundle'"
1). check for "assets" folder at : mkdir android\app\src\main\assets
if folder is not available ,create folder with name "assets" manually. and execute the Curl command in terminal.
2). Curl command: curl "http://localhost:8081/index.android.bundle?platform=android" -o"android/app/src/main/assets/index.android.bundle"
it will create the "index.android.bundle" file in assets folder automatically and resolved the issue.
3). react-native run-android
Chipping in an obvious suggestion that worked for me. Delete the app, restart the server and deploy again from your tooling to device.
I had this issue when i was trying to debug my code , (in using Toggle inspector in my android phone and react development ) , I did this simple steps :
- Open menu in phone ( where Reload is , I can open it by long press on back-button Or Shaking the phone ) enter image description here
- tap Dev Setting
- in Debugging -> Debug Server host & port for device should be empty Note : if you wrote port or anything here ( for debugging ) you should clear it.
I encountered this issue not with a real device, but with an emulator. I fixed it by running react-native start in the terminal before trying to run the code with android studio. I never encountered this issue when running react-native run-android in the terminal.
i resolve it by this cmd :
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
Comments
Post a Comment