ckknight / random-js

A mathematically correct random number generator library for JavaScript.
MIT License
603 stars 49 forks source link

feat: Add support for React Native #40

Open diosney opened 4 years ago

diosney commented 4 years ago

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);
Spacerat commented 3 years ago

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.

ApayRus commented 3 years ago

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

rawr51919 commented 3 years ago

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

ApayRus commented 3 years ago

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

rawr51919 commented 1 year ago

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

Spacerat commented 1 year ago

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.

rawr51919 commented 1 year ago

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