Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 283
gilsho Avatar asked Apr 05 '26 16:04

gilsho


1 Answers

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.

like image 130
Kriz Poon Avatar answered Apr 08 '26 04:04

Kriz Poon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!