5

I am attempting to learn React Native so I went ahead and followed all the steps in the Getting Started guide at the official documentation site. I have some experience with React so i wanted to expand on it.

I did wade in around 6 months ago for a simple 'hello world' app and everything worked fine for me on both the Android emulator and the IoS simulator. Did test on my device as well and had no issues. Since i got busy with other work i did not get back to it until now.

The error i get when i attempt the react-native run-android command:

    Metro Bundler ready.

Loading dependency graph, done.
error: bundling failed: Error: Unable to resolve module `react-native` from `/Users/arunmenon/Sites/reactnative/website/index.js`: Module `react-native` does not exist in the Haste module map or in these directories:
  /Users/arunmenon/Sites/reactnative/website/node_modules

This might be related to https://github.com/facebook/react-native/issues/4968
To resolve try the following:
  1. Clear watchman watches: `watchman watch-del-all`.
  2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.
  3. Reset Metro Bundler cache: `rm -rf /tmp/metro-bundler-cache-*` or `npm start -- --reset-cache`.  4. Remove haste cache: `rm -rf /tmp/haste-map-react-native-packager-*`.
    at ModuleResolver.resolveDependency (/Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:161:1460)
    at ResolutionRequest.resolveDependency (/Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:91:16)
    at DependencyGraph.resolveDependency (/Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/node-haste/DependencyGraph.js:272:4579)
    at dependencies.map.relativePath (/Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/DeltaBundler/traverseDependencies.js:376:19)
    at Array.map (<anonymous>)
    at resolveDependencies (/Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/DeltaBundler/traverseDependencies.js:374:16)
    at /Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/DeltaBundler/traverseDependencies.js:212:33
    at Generator.next (<anonymous>)
    at step (/Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:313)
    at /Users/arunmenon/Sites/reactnative/website/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:473
 BUNDLE  [android, dev] ./index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1), failed.

I tried all the suggested steps but the issue still persists. The error:

Error: Unable to resolve module `react-native` from `/Users/arunmenon/Sites/reactnative/website/index.js`: 
Module `react-native` does not exist in the Haste module map or in these directories:
  /Users/arunmenon/Sites/reactnative/website/node_modules

suggests that it looks like somehow there is some issue with picking up the path from node_modules. The first line in index.js is import { AppRegistry } from 'react-native'; and thats where the error is thrown.

The paths mentioned are valid and i do have react-native installed in the node_modules directory in the path.

I am developing on a Mac and the targeted device currently is Android. I am going the react-native CLI command route and not the create-react-native-app route.

I have Android Studio 3.1.3 (updated last night), Node 8.11.2, NPM 6.1.0, Watchman 4.9.0 (installed via Homebrew), Java JDK 1.8.0_144.

I followed the getting started guid to a T. Since i got t working earlier - around 6 months ago - i wonder why i get this now. I have the same Android Studio version as then(i updated Android Studio yesterday to rule out issues with having an older version. I get the same error with the new version as well) The difference between then and now:

  • React-native library version has obviously been bumped up from then
  • Java version, i recently updated it to the latest
  • Node and NPM versions are newer
  • Watchman also i think since Homebrew did update it

Everything else is the same. I tried a couple of fresh install as well - starting over from the beginning i.e but it didnt work.

My package.json:

{
  "name": "website",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.3.1",
    "react-native": "0.55.4"
  },
  "devDependencies": {
    "babel-jest": "23.2.0",
    "babel-preset-react-native": "4.0.0",
    "jest": "23.2.0",
    "react-test-renderer": "16.3.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

3 Answers 3

14

Please remove all react-native specific folders from "%appdata%\Temp" and then try below commands:

cd android
gradlew clean
cd.. and remove the node_modules folder
npm cache clean --force
npm install
npm start -- --reset-cache
react-native run-android

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the comments. The equivalent to a temp folder in Mac is Library/cache and the $TMPDIR as mentioned here: apple.stackexchange.com/a/131080. The first location had no react-native related directories but i found a few react references in yarn and typescript directories. I removed these directories. In the $TMPDIR structure there are no direct react-native references. I did find a hatsmap reference and a metro directory which i have removed. I am not sure which android directory you mean in cd android. Is it the sdk directory?
I also don't have the gradlew command available, at least when using via the terminal/prompt. I get -bash: gradlew: command not found. Should all this be done within Android Studio? I don't use Android Studio for development. I use Visual Studio Code alongwith either its inbuilt terminal or the globally available terminal for node/npm commands
All these commands are windows specific. There will be android folder in your project directory, go to that folder and open command prompt and run gradlew clean command there. That's it.
@arunmenon, yes you can use inbuilt or global terminal for running these commands. And for gradlew command, you have to be in android folder of your project, hope it will help you. Let me know if any issue
Ive managed to get it going at last. While i cannot exactly pinpoint which of the several actions i took worked - i tried various options over the weekend - what needs to go to the checklist is to try a new fresh install in a different directory. Deleting the project directory and re-creating it with the same name didn't work. A new directory name would be required. One good thing thats come out is that i seem to be somewhat nativescript. dev ready as well. Thanks for the answer - a am accepting this as the correct answer since the answer has valid steps.
|
3

The above didn't work for me on Mac OSX, but this one did: after npm install, run these:

# Clean cache
rm -rf $TMPDIR/react-*; rm -rf $TMPDIR/haste-*; rm -rf $TMPDIR/metro-*; watchman watch-del-all

# Start React-Native directly
react-native start --reset-cache

# Now run android/iOS in another tab
react-native run-android

Original post: https://github.com/facebook/react-native/issues/21490#issuecomment-427240356

2 Comments

ok. The project is shelved at the moment but this seems to be useful information. I guess ill refer back here in case i encounter the same issues in any upcoming React Native projects (i have one planned for the near future).
I am on a React Native project currently and the issue no longer appears in the version that i am using (0.57.8). So it is a combination of factors including having the latest version of the package.
0

In my case problem was newly installed package. so try with uninstall the npm package which you newly installed. npm uninstall --save

1 Comment

In my original issue it was a fresh install. No additional packages (besides the default packages installed by React Native) were added. But this solution is definitely valid in use-cases where there are existing packages and a new install triggered the issue. After all this while i definitely think my original issue was totally cache-related since even deleting and recreating the project with the same name had no effect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.