React Native错误“无法解析模块” react-native`”

时间:2018-06-30 04:37:00

标签: android react-native

我试图学习React Native,所以我继续进行并遵循官方文档站点Getting Started指南中的所有步骤。我对React有一定的经验,因此我想在此基础上进行扩展。

大约6个月前,我确实涉猎了一个简单的“ hello world”应用程序,并且在Android模拟器和IoS模拟器上一切正常。也在我的设备上进行了测试,没有任何问题。由于我忙于其他工作,所以直到现在我才重新开始。

尝试react-native run-android命令时出现错误:

    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.

我尝试了所有建议的步骤,但问题仍然存在。错误:

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

建议从node_modules提取路径似乎存在某种问题。 index.js的第一行是import { AppRegistry } from 'react-native';,这就是引发错误的地方。

提到的路径是有效的,并且我确实在该路径的node_modules目录中安装了react-native。

我正在Mac上进行开发,目标设备当前是Android。我正在使用react-native CLI命令路由,而不是create-react-native-app路由。

我有Android Studio 3.1.3(昨晚更新),Node 8.11.2,NPM 6.1.0,Watchman 4.9.0(通过Homebrew安装),Java JDK 1.8.0_144。

我遵循了T的入门指南。由于我早于大约6个月前开始工作,所以我不知道为什么现在得到这个。我使用的是相同的Android Studio版本(我昨天更新了Android Studio,以排除使用旧版本的问题。新版本也出现了相同的错误)当时与现在之间的区别:

  • 从那时起,本机库版本明显被提高
  • Java版本,我最近将其更新为最新版本
  • Node和NPM版本较新
  • 我也认为守望者自从Homebrew进行了更新

其他所有内容都相同。我也尝试了几次全新安装-从头开始,即它没有用。

我的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 个答案:

答案 0 :(得分:2)

请从“%appdata%\ Temp”中删除所有本机特定的文件夹,然后尝试以下命令:

cd android
gradlew clean
cd ..并删除node_modules文件夹
npm缓存清理--force
npm install
npm start--重置缓存
react-native运行Android

答案 1 :(得分:1)

上面的内容在 Mac OSX 上对我不起作用,但是它可以做到:npm install之后,运行以下命令:

# 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

原始帖子:https://github.com/facebook/react-native/issues/21490#issuecomment-427240356

答案 2 :(得分:0)

就我而言,问题是新安装的软件包。因此,请尝试卸载新安装的npm软件包。 npm卸载--save