如何检测模块中的React Native打包程序?

我有一个模块,我用于节点和(现在)反应原生。 我想有select地导出代码取决于它将运行在哪个平台上。 如果你能在设备上运行,你已经解决了这个问题。

模块:

if(!react_native){ exports.fs = require('fs'); } exports.print = function(str){ console.log(str); } 

在设备上:

 var m = require('module'); m.print("hello world."); 

有没有办法做到这一点?

我不想创build两个单独的模块,只有在不需要index.js时才会有所不同。

谢谢!

一个简单的检查 – 加载基本包:

 var isNative = false; var Platform; try { Platform = require('react-native').Platform; isNative = true; } catch(e) {} if (isNative) { console.log(Platform.OS, Platform.Version); } else { console.log('node'); }