我一直在尝试测试我创建的react native模块,但是,我很难使npm包正确链接,并且不断出现错误:
Module 'mySDK' does not exist in the Haste module map
我尝试过的事情:
npm install file:pathToPackage --save
这使我可以使用以下方式访问我的react native模块:
const mySDK = NativeModules.mySDK;
但是,我希望能够通过导入来导入导出的JS:
import {mySDK} from 'react-native-mysdk';
添加此导入文件时,出现上述错误,我也尝试将module:react-native-mysdk
添加到我的babel.config.js
中,并且相同的错误继续。
我采用的另一种方法是使用npm link
,这两种方法都会产生相同的错误。
作为参考,我正在尝试从模块中导出package.json
和一些JS。
package.json
{
"name": "react-native-mysdk",
"version": "0.0.1",
"description": "",
"main": "./js/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"flow": "flow",
"lint": "node_modules/.bin/eslint . --max-warnings=0 src/",
"lint-fix": "node_modules/.bin/eslint . --fix src/"
},
"keywords": [
"react-native"
],
"author": "TODO",
"license": "TODO",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-module-resolver": "^3.1.3",
"babel-preset-flow": "^6.23.0",
"babel-preset-react-native": "2.1.0",
"flow-typed": "^2.3.0",
"flow-bin": "^0.92.0",
"eslint": "^4.18.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-import-resolver-babel-module": "^4.0.0",
"eslint-plugin-flowtype": "^2.46.1",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"eslint-plugin-react-native": "^3.2.1"
},
"dependencies": {
"react": "16.6.3",
"react-native": "0.58.3"
}
}
js/index.js
/**
* @flow
* @format
*/
'use strict';
module.exports = {
get mySDK() {
return require('./mySDK');
},
};
js/mySDK.js
module.exports {
test(): Promise<string> {
return Promise.resolve("test");
}
};
这也是我的示例应用程序的package.json
:
{
"name": "sample",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-module-resolver": "^3.1.3",
"babel-preset-flow": "^6.23.0",
"babel-preset-react-native": "2.1.0",
"flow-typed": "^2.3.0",
"flow-bin": "^0.92.0",
"react": "16.6.3",
"react-native": "0.58.3",
"react-native-mysdk": "file:../.."
},
"devDependencies": {
"babel-jest": "16.0.0",
"babel-preset-react-native": "1.9.0",
"jest": "16.0.2",
"jest-react-native": "16.0.0",
"react-test-renderer": "15.3.2"
},
"jest": {
"preset": "react-native"
}
}
我尝试了各种操作,例如重新启动npm和清除缓存,但似乎无济于事。