gilsho
gilsho

Reputation: 101

Using NodeJS Crypto module in browser with webpack

I am writing a javascript REPL that is intended to run in the browser and execute nodejs crypto functions. My project is in ReactJS and I am using webpack to bundle all the modules and dependencies together. I am trying to include the builtin node crypto module in the bundle file produced by webpack and I am unable to do so. Webpack seems to replace the builtin crypto module with the crypto-browserify using the NodeSourcePlugin plugin and the node-libs-browser package. When I disable these checks I get an error during the compilation process complaining that the crypto module cannot be resolved.

Is there a way to circumvent this behavior and include the nodejs crypto javascript code? Or is there something inherent about the crypto library that prohibits it from being included.

I tried using webpack.ProvidePlugin to require the node crypto module at compile time and then assigning it explicitly but that didn't work.

My webpack config was generated by ReactJS. It is too long to include but it should be pretty boilerplate.

Upvotes: 9

Views: 15598

Answers (1)

Kriz Poon
Kriz Poon

Reputation: 169

NodeJS crypto module is a native module that is written in C++. There is no way to bundle it in your ReactJS app to be run inside a browser.

You'll need a pure JavaScript package. Maybe take a look at crypto-js.

Upvotes: 8

Related Questions