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 Message:
Error: Unable to resolve module ./index from node_modulesreact-nativescripts/. : The module ./index could not be found from node_modulesreact-nativescripts/. Indeed, none of these files exist.
Permanent Solution for Error: Unable to resolve module ./index
from `node_modulesreact-nativescripts/:
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.