【发布时间】:2018-07-04 02:11:20
【问题描述】:
我正在尝试在我的 react-native 应用程序中使用节点模块,我在这里采用ReactNativify 方法。
我现在一切就绪,我可以正常加载加密包。但是,当我添加 eth-lightwallet 时,事情变得越来越奇怪。
自从我添加了那个包之后,npm 就没有安装任何依赖项。这意味着我必须手动添加它们。每次我安装与 eth-lightwallet 相关的依赖项时,都会卸载该模块。虽然乏味且烦人,但我希望它可以阐明我当前的问题。
现在我遇到了Can't find variable: Buffer,它被扔到标准库的 util 文件夹中。我查看了代码,它正在从全局命名空间访问 Buffer。事情是,我正在将 Buffer 导入全局命名空间。下面看看我的 global.js
// Inject node globals into React Native global scope.
global.Buffer = require('buffer').Buffer;
global.process = require('process');
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';
// Needed so that 'stream-http' chooses the right default protocol.
global.location = {
protocol: 'file:',
};
// Don't do this in production. You're going to want to patch in
// https://github.com/mvayngrib/react-native-randombytes or similar.
global.crypto = {
getRandomValues(byteArray) {
for (let i = 0; i < byteArray.length; i++) {
byteArray[i] = Math.floor(256 * Math.random());
}
},
};
我的猜测是在加载这个全局变量之前正在评估标准库,因此会引发错误。
【问题讨论】:
-
我也有同样的问题,你试过用 rn-nodeify 包代替 react nativify 吗?
标签: node.js react-native npm global-variables