当我使用Xcode运行我的react-native应用程序时,在应用程序开始加载后,我看到了这个错误:
"Requiring unknown module "react". If you are sure the module is there, try restarting the packager."
我的package.json内容是:
{
"name": "the_golf_mentor",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"dependencies": {
"react-native": "^0.21.0",
"react-native-button": "^1.4.2",
"react-native-cookies": "^0.0.5",
"react-native-db-models": "^0.1.3",
"react-native-navbar": "^1.2.1",
"react-native-side-menu": "^0.18.0",
"tcomb-form-native": "^0.3.3"
}
}
我尝试重新启动软件包管理器,并且还删除了node_module文件夹并完成了新的npm install
。我该如何解决这个问题?
答案 0 :(得分:6)
我在启用热重新加载后,然后降级我的反应版本以支持我正在使用的库。
模拟器>重置内容和设置
让它再次运行。
答案 1 :(得分:3)
React Native change object
位于function Request(options, callback) {
HTTPRequest = XMLHttpRequest;
var xhr = new HTTPRequest(),
method = options.method || "POST",
url = options.url,
headers = options.headers;
// make a request
xhr.open(method, url, true);
// set headers
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
// send stringify data
if (options.body && method == "POST" || method == "PUT") {
if (typeof options.body === 'object') {
console.log("sending in data: options.body");
xhr.send(JSON.stringify(options.body));
} else {
xhr.send(options.body);
}
} else {
xhr.send();
}
// collect response
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var data = xhr.responseText;
try {
data = JSON.parse(data);
} catch (e) {
console.error('Could not parse the response received from the server.');
}
if (xhr.status >= 200 && xhr.status < 300) {
callback(null, data);
} else {
callback(data, null);
}
}
};
}
var options = {
url: 'https://api.contentstack.io/v2/content_types/the_lazy_goose/entries/',
method:'POST',
headers:{
access_token: 'xxxxxxx', //put auth token here
api_key: 'xxxxxxx'
},
body: {
"entry": {
"title": "examplexx1",
"url": "/example10xxx1",
"multi_line": "multiline...!",
"_comment": "example comment"
}
}
};
模块而不是React
(对于React JS)。在这种情况下,通过以下方式导入react-native
和其他React Native模块:
react
答案 2 :(得分:0)
您应该在反应原生项目中React
react-native
,而不是react
。{/ p>
var React = require('react-native');
答案 3 :(得分:0)
事实证明,错误消息是我在调试器中收到的临时消息。当我在调试器中按下几次继续时,我在模拟器窗口中收到一条消息,表示我没有正确注册该应用程序,即我的AppRegistry.registerComponent
电话无效。
我怀疑这是unknown module 'react'
错误消息的异常原因。在许多情况下,neciu's
答案可能更有用,所以我也赞成了它。