Skip to content Skip to sidebar Skip to footer

React Native Can't Find Module ./index On Fresh Initialized App

I issued react-native init MyApp and react-native run-android Metro server started but when phone request data from it, it crashed giving Error: Unable to resolve module `./index`

Solution 1:

SolutionGithub Ref: #23908 (comment)

Metro server instance is started by runAndroid.js from @react-native-community module if asked for react-native run-android

Problem is with Working Directory, Metro instance is launched with wrong working directory and no projectRoot is passed in launchPackager.bat

Threre are two fixes for this issue, apply only one of the below

Update node_modules\react-native\scripts\launchPackager.bat file.

@echo off
title Metro Bundler
call .packager.bat

:: delete this line
node "%~dp0..\cli.js" start 

:: Add this line
node "%~dp0..\cli.js" start--projectRoot ../../../ 

pause
exit

We are giving project root path to Metro instance here via projectRoot argument,

Or in \node_modules\@react-native-community\cli\build\commands\runAndroid\runAndroid.js edit this,

const procConfig = {

    // delete this line    
    cwd: scriptsDir

    // add this line
    cwd: process.cwd()

};

We are launching Metro Server with working directory to our project root

For more info see startServerInNewWindow() function in \node_modules\@react-native-community\cli\build\commands\runAndroid\ranAndroid.js, it is passing react-native directory rather than project root in third argument of spawn().

It worked, hope this will help you as well

Post a Comment for "React Native Can't Find Module ./index On Fresh Initialized App"