manjeet
manjeet

Reputation: 1517

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` 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:

Machine has newly installed node, npm and it's modules so no cache issues, but then what is preventing react native to give even its first glimpse?

Upvotes: 1

Views: 1097

Answers (1)

manjeet
manjeet

Reputation: 1517

Solution Github 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

Upvotes: 2

Related Questions