_crypto 未定义

_crypto is not defined

我已经对此进行了大约一个小时的挖掘,但无法弄清楚是什么导致了这个问题。我正在使用 webpack、browser-sync,并使用 react-redux 做出反应。这是来自控制台的错误:

Uncaught ReferenceError: _crypto is not defined
    at eval (eval at <anonymous> (bundle.js:881), <anonymous>:5:11)
    at Object.eval (eval at <anonymous> (bundle.js:881), <anonymous>:20:3)
    at eval (eval at <anonymous> (bundle.js:881), <anonymous>:21:30)
    at Object.<anonymous> (bundle.js:881)
    at __webpack_require__ (bundle.js:20)
    at Object.eval (eval at <anonymous> (bundle.js:875), <anonymous>:3:11)
    at eval (eval at <anonymous> (bundle.js:875), <anonymous>:46:30)
    at Object.<anonymous> (bundle.js:875)
    at __webpack_require__ (bundle.js:20)
    at Object.eval (eval at <anonymous> (bundle.js:833), <anonymous>:4:14)

我也刚遇到。我的解决方案是升级 crypto-browserify 依赖项:

npm install crypto-browserify@3.11.0

编辑 1:由于我不太明白的原因,这只是暂时解决了问题。一旦我重新启动 webpack,它又停止工作了。

编辑 2:这只发生在我的一个项目中,该项目相当复杂,因此很难确定两者之间的确切区别,但作为临时修复,你可以声明变量:

window._crypto = null;

这不是很好,但希望这是暂时的。

那是一个超级过时的 crypto browserify 版本,rng.js 至少从大约一年前发布的 3.9.8 开始就已经消失了,有些东西需要过时的版本。

我遇到了同样的问题,找到原因了。

当您使用 webpack 1.x 时,node-libs-browser@7.0crypto-browserify@3.3.0(可能更早)将同时安装。

所以,如果你直接require('crypto'),webpack会使用@3.3.0,其中有rng.js,而不是你自己安装的最新版本。

我试过的解决方案就是npm install node-libs-browser --save,npm会在webpack中更新,会找到@3.11.0。