反应本机图像选择器在运行时崩溃-Metro Bundler错误

时间:2019-09-05 00:14:03

标签: ios react-native package-managers react-native-image-picker metro-bundler

我想在我的应用程序中使用“ react-native-image-picker”。导入并遵循github指令中的安装说明后。我在运行时遇到地铁捆绑器崩溃。它似乎找不到“ react-image-picker”或其依赖项?

我尝试初始化一个新项目,并且仅安装图像选择器。我试过在根目录中运行“ react-native链接”和“ react-native链接react-native-image-picker”。我尝试过用Expo初始化项目。我检查了Metro声称缺少的目录,它们对我来说看起来很好。

import React from 'react';
import {
  StyleSheet,
  ScrollView,
  View,
  Text,
  Button
} from 'react-native';
import ImagePicker from 'react-native-image-picker';

const options = {
  title: 'Select Avatar',
  customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
  storageOptions: {
    skipBackup: true,
    path: 'images',
  },
};

const App = () => {
  return (
    <Button title="Select Image" onPress={() => {
      ImagePicker.showImagePicker(options, (response) => {console.log('Response = ', response);})
    }} />
  );
};

错误:捆绑失败:错误:尝试从文件react-native-image-picker解析模块/Users/owenmorris/Documents/GitHub/fecebook/App.js时,成功找到了包/Users/owenmorris/Documents/GitHub/fecebook/node_modules/react-native-image-picker/package.json。但是,该程序包本身指定了一个main无法解析的模块字段(/Users/owenmorris/Documents/GitHub/fecebook/node_modules/react-native-image-picker/src/index.ts。实际上,这些文件都不存在:

  • /Users/owenmorris/Documents/GitHub/fecebook/node_modules/react-native-image-picker/src/index.ts(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)
  • /Users/owenmorris/Documents/GitHub/fecebook/node_modules/react-native-image-picker/src/index.ts/index(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx) 在ResolutionRequest.resolveDependency(/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:65:15) 在DependencyGraph.resolveDependency(/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/node-haste/DependencyGraph.js:283:16) 在Object.resolve(/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/lib/transformHelpers.js:264:42) 在/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/DeltaBundler/traverseDependencies.js:399:31 在Array.map() 在resolveDependencies(/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/DeltaBundler/traverseDependencies.js:396:18) 在/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/DeltaBundler/traverseDependencies.js:269:33 在Generator.next() 在asyncGeneratorStep(/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/DeltaBundler/traverseDependencies.js:87:24) 在_next(/Users/owenmorris/Documents/GitHub/fecebook/node_modules/metro/src/DeltaBundler/traverseDependencies.js:107:9) BUNDLE [ios,dev] ./index.js░░░░░░░░░░░░░░░░0.0%(0/1),失败。

2 个答案:

答案 0 :(得分:0)

答案是here

简单地是:

  1. 第二个解决方案

从lib / common / js导入库 从'react-native-image-picker / lib / commonjs'导入ImagePicker;

答案 1 :(得分:0)

我在最新版本中也遇到了类似的问题。我将版本更改为0.28.0。

npm install react-native-image-picker@0.28.0
npx react-native link react-native-image-picker

请查看它是否适合您。