React Native-使用我制作的自定义组件时出现错误(错误:undefined无法解析模块<MyModule>)

编码伤害

我制作了一个名为“ react-native-weekly-calendar”的自定义组件,并试图将其发布到开源社区,但是我想先对其进行测试。当我尝试通过测试它时npm install --save ../<component_name>,它将引发错误。

我的目录结构:

- react-native-weekly-calendar (folder)
    - index.js
    - package.json
    - src (folder)
        - Locale.js
        - Style.js

index.js:

import React, { useState, useEffect, useRef } from 'react';
import { Text, View, ScrollView, TouchableOpacity, TouchableWithoutFeedback, Modal, Platform, ActivityIndicator } from 'react-native';
import PropTypes from 'prop-types';
import moment from 'moment/min/moment-with-locales';
import DateTimePicker from '@react-native-community/datetimepicker';
import { FontAwesome } from 'react-native-vector-icons';
import { applyLocale, displayTitleByLocale } from './src/Locale';
import styles from './src/Style';

const WeeklyCalendar = props => {
    ...
}

export default WeeklyCalendar;

package.json:

{
  "name": "react-native-weekly-calendar",
  "version": "0.1.0",
  "description": "Weekly Calendar component for React Native",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/codeinjuice/react-native-weekly-calendar.git"
  },
  "keywords": [
    "react",
    "reactnative",
    "react-native",
    "react-native-component",
    "calendar",
    "weeklycalendar",
    "weekly-calendar",
    "scheduler",
    "datepicker",
    "date-picker"
  ],
  "author": "codeinjuice",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/codeinjuice/react-native-weekly-calendar/issues"
  },
  "homepage": "https://github.com/codeinjuice/react-native-weekly-calendar#readme",
  "dependencies": {
    "moment": "^2.24.0",
    "prop-types": "^15.5.7",
    "@react-native-community/datetimepicker": "~2.1.0",
    "react-native-vector-icons": "~6.6.0"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "metro-react-native-babel-preset": "^0.58.0",
    "react": "~16.9.0",
    "react-native": "0.61.4"
  }
}

这是我对以上文件进行测试的方式:

$ npx react-native init sample
$ cd sample
$ npm install --save ../react-native-weekly-calendar
$ vim App.js

然后我添加import WeeklyCalendar from 'react-native-weekly-calendar';了App.js。终于我跑了

$ npx react-native run-ios

在模拟器中,出现此错误:

无法从“ App.js”解析模块“ react-native-weekly-calendar”:在项目内找不到react-native-weekly-calendar。

我在package.json???中设置了我的依赖项设置错误吗?

我不明白为什么当它显然位于node_modules文件夹内时,总是说找不到该组件。

任何建议都欢迎!

编码伤害

编辑:我将react-native从0.60。*升级到0.61.4,它也与npm一起使用。

我使用的原始答案$ yarn add ../react-native-weekly-calendar

而不是$ npm install --save ../react-native-weekly-calendar它起作用了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档