需要未知模块“crypto"在本机环境中 [英] Requiring unknown module "crypto" in react-native environment
问题描述
我正在使用 react-native 编写一个简单的 Twitter 应用程序.使用 twit 模块获取 Twitter 提要和流.下面是代码,它工作正常节点.但是,当包含在我的 react-native 应用程序中时,看到错误需要未知模块加密"".依赖似乎是 myapp->twit->oauth->crypto(这是节点 v0.12.2 的一部分).有什么建议可以让它在本机环境中工作吗?
I'm writing a simple Twitter app using react-native. Using twit module to get twitter feeds and stream. Below is the code, it works fine node. However, when included into my react-native app, seeing error "Requiring unknown module "crypto"". Dependency seems to be myapp->twit->oauth->crypto (thats part of node v0.12.2). Any suggestions to get this working inside react-native environment?
var Twit = require('twit')
var T = new Twit({
consumer_key:''
, consumer_secret:''
, access_token:''
, access_token_secret:''
})
var filtered_tweets=[];
var error;
var isSuccess=false;
function getTweets(searchString){
T.get('search/tweets',{q:searchString, count:100}, getResponse);
}
function getResponse(err,data,response){
if(err) {
handleGetErr(err);
}
handleGetData(data.statuses);
}
function handleGetErr(err){
enter code here
error = err;
}
function handleGetData(data){
data.map(function(tweet){
var twit={
twit:tweet.id,
created_at:tweet.created_at,
text:tweet.text,
retweet_count:tweet.retweet_count,
favorite_count:tweet.favorite_count
};
filtered_tweets.push(twit);
});
console.log(filtered_tweets);
isSuccess=true;
}
getTweets("@sahaswaranamam");
module.exports = getTweets;
![附][2]
推荐答案
crypto
模块是一个内置的 Node 模块;React Native 在 JavaScriptCore(在设备或模拟器上)和 Chrome 本身(在使用 Chrome 调试时)上运行 JS,因此依赖于内置 Node.js 模块的模块将无法工作.请参阅 React Native 的JavaScript 运行时部分文档了解更多信息.
The crypto
module is a built-in Node module; React Native runs JS on JavaScriptCore (when on the device or simulator) and on Chrome itself (when using Chrome debugging), so modules that depend on built-in Node.js modules won't work. See the JavaScript Runtime section of the React Native docs for more info.
我不确定集成到 React Native 应用程序会有多困难,但是像 Browserify 这样的浏览器模块捆绑器通常有核心 Node.js 模块的浏览器版本,例如 这个用于 crypto
.
I'm not sure how hard it would be to integrate into a React Native app, but browser module bundlers like Browserify often have browser versions of core Node.js modules, like this one for crypto
.
这篇关于需要未知模块“crypto"在本机环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!