React Native-使用我制作的自定义组件时发生错误(错误:undefined无法解析模块 )

问题描述 投票:2回答:1

我制作了一个名为“ 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

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

$ npx react-native run-ios

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

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

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

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

欢迎提出任何建议!

reactjs react-native npm publish package.json
1个回答
0
投票

我使用了$ yarn add ../react-native-weekly-calendar

代替$ npm install --save ../react-native-weekly-calendar,它起作用了。

© www.soinside.com 2019 - 2024. All rights reserved.