_crypto is not defined

11.6k views Asked by At

I've been digging on this for about an hour, but can't figure out what's causing the issue. I'm using webpack, browser-sync, and react with react-redux. Here's the error from the console:

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)
3

There are 3 answers

1
mrzapp On BEST ANSWER

I just encountered this too. The solution for me was to upgrade the crypto-browserify dependency:

npm install [email protected]

EDIT 1: For reasons I don't quite grasp, this only fixed the problem temporarily. As soon as I restarted webpack, it stopped working again.

EDIT 2: This happens only in one of my projects, which is rather complex, so it's hard to pin down exactly what the differences are between the two, but as a temporary fix, you can just declare the variable:

window._crypto = null;

It's not pretty, but hopefully this will be temporary.

0
Calvin On

that is a super out of date version of crypto browserify, rng.js has been gone since at least 3.9.8 which was released almost a year ago, something is requiring an out of date version.

0
Edisonator On

I've had the same problem, and found the reason.

As you use webpack 1.x, the [email protected] with [email protected] (maybe earlier) will be installed at the same time.

So, if you just require('crypto') directly, webpack will use the @3.3.0, which has the rng.js, and not the newest version you installed by yourself.

image

The solution I tried is just npm install node-libs-browser --save, and npm will update it in webpack, and @3.11.0 will be found.