【发布时间】:2019-07-25 11:00:22
【问题描述】:
我是 react-native 的新手,我第一次创建了自己的模块 (npm)。
我尝试为常用组件创建自己的模块(npm),并且单独工作正常。但是,一旦我安装,将其链接到我们的应用程序中,并尝试使用这些组件,它会给出如下错误:
Unable to resolve module `<Module-name>` from `<file>`: Module `<Module-name>` does not exist in the Haste module map.
(注意:我仅使用 Android Emulator 进行了测试)
我已按照以下步骤创建模块和测试应用程序
对于模块
react-native init <module-name>- 在main
index.js中添加组件的简单代码 -
并像这样设置 pacakge.json:
{ "name": "<module-name>", "version": "0.0.1", "private": true, "main": "index.js", "scripts": { "start": "react-native start", "test": "jest", "lint": "eslint .", "build": "echo 'build script executed'" }, "dependencies": { "react": "16.8.6", "react-native": "0.60.4" },
现在我检查这个组件(react-native run-android),它可以正常工作。
现在我尝试通过以下步骤在应用中使用
测试应用
- 创建新应用 (
react-native init <app-name>) -
使用完整路径安装创建的 npm;
npm install <full path of component> 现在我尝试
react-native link <full path of component>,我也尝试过简单的react-native link,但没有任何反应。-
现在我使用
react-native run-android运行应用程序,但是每次它都会给出同样的错误;Unable to resolve module <created-module name>
测试应用package.json
{
"name": "<app-name>",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"react": "16.8.6",
"react-native": "0.60.4",
"react-native-my-library": "^1.0.2",
"<created-module-name>": "file:../<created-module-name>"
},
"devDependencies": {
"@babel/core": "7.5.5",
"@babel/runtime": "7.5.5",
"@react-native-community/eslint-config": "0.0.3",
"babel-jest": "24.8.0",
"eslint": "6.1.0",
"jest": "24.8.0",
"metro-react-native-babel-preset": "0.54.1",
"react-test-renderer": "16.8.6"
},
"jest": {
"preset": "react-native"
}
}
创建的模块安装成功,但我认为它没有正确链接,它会报错。
那么这里有什么问题呢?
请给我建议/帮助。
谢谢。
【问题讨论】:
-
你的模块有android&ios文件夹吗?大多数时候
react-native link不适用于本机库。你必须自己做。 -
@firats 模块适用于 android 和 ios,实际上它不是特定的 android 和 ios,我也尝试过 react-native 链接
但在 android 中没有任何改变,比如 gradlefile,设置。毕业典礼等。 -
你找到解决办法了吗?
标签: react-native npm