首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用我创建的自定义组件时获得一个错误(错误:未定义无法解析模块<MyModule>)

使用我创建的自定义组件时获得一个错误(错误:未定义无法解析模块<MyModule>)
EN

Stack Overflow用户
提问于 2020-03-19 07:08:38
回答 1查看 845关注 0票数 2

我制作了一个名为“react原生-每周日历”的自定义组件,我试图将它发布到开源社区,但我想先测试它。当我尝试用npm install --save ../<component_name>测试它时,它会抛出一个错误。

我的目录结构:

代码语言:javascript
复制
- react-native-weekly-calendar (folder)
    - index.js
    - package.json
    - src (folder)
        - Locale.js
        - Style.js

index.js:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
  "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"
  }
}

下面是我如何使用上面的文件进行测试:

代码语言:javascript
复制
$ 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。最后我跑了

代码语言:javascript
复制
$ npx react-native run-ios

在模拟器中,我得到了以下错误:

无法从‘App.js’中解析模块‘react本机-周-日历’:无法在项目中找到react本机-周日历。

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

我不明白为什么当组件清楚地位于node_modules文件夹中时,它总是说组件没有找到。

欢迎任何建议!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-23 06:53:11

编辑:我升级了react-从0.60.*升级到0.61.4,它也与npm一起工作.

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60752371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档