random-js icon indicating copy to clipboard operation
random-js copied to clipboard

feat: Add support for React Native

Open diosney opened this issue 5 years ago • 7 comments

Currently if used on React Native, it throw an error stating that crypto module wasn't found.

Tested with the following code:

import { integer, MersenneTwister19937 } from 'random-js';

const engine = MersenneTwister19937.autoSeed();
return integer(min, max)(engine);

diosney avatar Jun 09 '20 16:06 diosney

I just ran into this too... my solution was to create this https://www.npmjs.com/package/random-js-no-node

Definitely a massive hack, but it worked!

I honestly don't know much about modules/packaging/etc, but the basic problem seems to be this: in the browser, random-js successfully makes 'crypto' an optional dependency, but somehow in the context of React Native, it's not. I imagine that something sees the require("crypto") call in the node engine, and decides that crypto is a required dependency.

Spacerat avatar Oct 29 '20 05:10 Spacerat

I faced the same issue. Please share more examples how to solve.

ApayRus avatar Jan 05 '21 07:01 ApayRus

I would suggest using a polyfill or similar that hooks to the module crypto-browserify

rawr51919 avatar Feb 04 '21 12:02 rawr51919

I commented code with 'crypto' and everything still works.

ApayRus avatar Feb 04 '21 14:02 ApayRus

PR #65 may provide a fix for this issue Also @Spacerat you'll likely need to update your repo once it merges because it will be a HUGE, sorely needed update

rawr51919 avatar Sep 16 '23 14:09 rawr51919

That's interesting - if #65 fixes this issue, I think my repo would be made redundant? In which case I wonder if it's better to deprecate it than keep it updated.

Spacerat avatar Sep 20 '23 20:09 Spacerat

That's interesting - if #65 fixes this issue, I think my repo would be made redundant? In which case I wonder if it's better to deprecate it than keep it updated.

yeah, as there's ways to do the same thing as what random-js-no-node does now with webpack 5+ polyfills

rawr51919 avatar Sep 20 '23 21:09 rawr51919