Since we all know react native has recently launched its newly more compatible 0.59.2 version with some basic changes. But after upgrading to new version or creating new react native project we all have faced a common error. The error is coming from the Metro server instance. When we run our react native project then it will launched with wrong working directory and no project root is passed in launchPackager.
Screenshot of Error:
Error: Unable to resolve module ./index from \node_modules\react-native\scripts/. : The module ./index could not be found from \node_modules\react-native\scripts/. Indeed, none of these files exist.
Permanent Solution for Error: Unable to resolve module ./index
from `\node_modules\react-native\scripts/:
1. Goto Your_React_Native_Project -> node_modules -> react-native -> scripts -> launchPackager.bat . Open the launchPackager.bat file in Notepad or Notepad ++ editor.
2. Put below line of code in launchPackager.bat file.
1 |
node "%~dp0..\cli.js" start --projectRoot ../../../ |
Source code of launchPackager.bat file after making changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
:: Copyright (c) Facebook, Inc. and its affiliates. :: :: This source code is licensed under the MIT license found in the :: LICENSE file in the root directory of this source tree. @echo off title Metro Bundler call .packager.bat node "%~dp0..\cli.js" start node "%~dp0..\cli.js" start --projectRoot ../../../ pause exit |
3. Now we need to give the project root path to metro instance object. So GoTo React_Native_Project -> node_modules -> @react-native-community -> cli -> build -> commands -> runAndroid.js . Open the runAndroid.js file in Notepad editor.
4. Find cwd: scriptsDir in const procConfig block.
1 2 3 |
const procConfig = { cwd: scriptsDir }; |
5. Replace cwd: scriptsDir with cwd: process.cwd() code & save the file.
1 2 3 4 5 |
const procConfig = { cwd: process.cwd() }; |
6. Now goto your project’s main root folder in command prompt or Terminal and execute react-native run-android command. You’ll see the magic your project will smoothly run without any error in emulator or real device.
This does not solve the problem. Still having the same error. Only solution that is working is ‘npm start — –reset-cache’ , but this is not the permanent solution
Thanks for comment but i have tried it and its working fine for me . Could you send me your project on [email protected] i will check it.
Thank you mate! Your solution fixed the error!
Welcome Mariyan 🙂 .
Thank you I did and succeeded
Welcome 🙂 .
Thanks, solved my problem
Welcome Jobie 🙂 ..
just worked for me… thanks 🙂
Welcome Sunny 🙂 .
this is work for me thanks
React_Native_Project -> node_modules -> @react-native-community -> cli -> build -> commands -> runAndroid-> runAndroid.js on line 248
Hi folks, I have tried with react native 0.59.3(latest) and it works fine and no need to made any changes in the launchPackager.bat file.
Thanks for notifying us Pankaj 🙂 .
Still not worked for me
Yoburaj what error you have facing ?
do you have any social media that i can contact?
Yes you can contact me on my Instagram page.
could not work for me.
React_Native_Project -> node_modules -> @react-native-community -> cli -> build -> commands -> runAndroid->
i do not have cli but have cli-platform-andriod. even if i tried on this in runAndriod i do not have runAndriod.js
its still not working for me