node.js - 如何检测模块中的 React Native 打包器?

标签 node.js npm react-native

我有一个用于 Node 和(现在)react-native 的模块。我想根据要运行的平台有选择地导出代码。如果您可以让它在设备上运行,那么您就解决了问题。

模块:

if(!react_native){
    exports.fs = require('fs');
}

exports.print = function(str){ console.log(str); }

在设备上:

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

有什么办法可以做到这一点吗?

如果不需要,我不想创建两个仅在 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');
}

关于node.js - 如何检测模块中的 React Native 打包器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035332/

相关文章:

node.js - 如何在 GraphQL 模式中使用 'Decimal' 作为字段类型?

javascript - 使用 request-promise 嵌套异步请求

node.js - 更新npm并安装docpad后出现权限错误

react-native - 设置其依赖的状态后,使用usecallback定义的调用函数

javascript - React-Native:应用程序尚未注册错误

node.js - webpack-dev-middleware 与 create-react-app

node.js - Nodejs - 使用选项 --data 来自文件的 Rest api 调用

node.js - npm install 在提取 faker 时挂起

node.js - NPM 7.0.0 的工作空间是否需要 Lerna?

javascript - 在另一个内部调用一个异步函数的最佳方式是什么?