React Native - Get an error when using a custom component that I made (Error: undefined Unable to resolve module <MyModule>)
React Native - Get an error when using a custom component that I made (Error: undefined Unable to resolve module <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
然后我在App.js中添加了import WeeklyCalendar from 'react-native-weekly-calendar';
。
最后我运行
$ npx react-native run-ios
在模拟器中,我得到这个错误:
Unable to resolve module 'react-native-weekly-calendar' from 'App.js':react-native-weekly-calendar could not be found within the project.
我是不是在 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
并且有效。
我制作了一个名为 "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
在模拟器中,我得到这个错误:
Unable to resolve module 'react-native-weekly-calendar' from 'App.js':react-native-weekly-calendar could not be found within the project.
我是不是在 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
并且有效。