Uuid: React Native require error

Created on 9 Jan 2016  路  19Comments  路  Source: uuidjs/uuid

require('node-uuid') or import uuid form 'node-uuid' results in a Unable to resolve module crypto error.

If I switch out require('crypto') with require('crypto-js'), and specifically include the crypto-js library instead, it works fine. Are we able to look at swapping out crypto with crypto-js?

Most helpful comment

I hope Robert will get time to merge it, meanwhile I created little something as a temporary replacement: https://www.npmjs.com/package/react-native-uuid

All 19 comments

+1

+1

As a work around, can you override the dependency higher up?

require('crypto-js');
require.cache[require.resolve('crypto')] = require.cache[require.resolve('crypto-js')]
var uuid = require('uuid');

That should be fine for a workaround, more thinking if it's better to have crypto-js as a dependency rather than crypto

Does crypto-js defer to node-native's implementation? Perhaps an intermediary, similar to isomorphic-fetch That simply points to one or the other depending on environment/build.

It looks to be a seperate implementation, doesn't have crypto as a dependency. I like the concept on environment/build setting, could we swap the dependency based on browser/node vs React Native?

+1

When I try to use crypto-js instead of crypto it seems to revert to using Math.random() and gives me a warning. Does this happen to anyone else?

Yea - happens to me too.

+1

+1

Is there any solution to this problem?
https://github.com/defunctzombie/node-uuid manages to avoid this problem, I'm not sure how.

FYI to all: @broofa admits that he is sort of an "absentee landlord" for this repo. So there probably won't be many fixes to it anytime soon. See https://github.com/broofa/node-uuid/issues/116 for discussion and alternatives.

馃憤

@broofa you should add some contributor if you don't have much time to merge some PRs or fix some issues

I hope Robert will get time to merge it, meanwhile I created little something as a temporary replacement: https://www.npmjs.com/package/react-native-uuid

It can be temporarily resolved by using babel-plugin-module-resolver.
Install babel-plugin-module-resolver

$ npm install --save-dev babel-plugin-module-resolver

And crypto-js

$ npm install --save crypto-js

Edit .babelrc

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["module-resolver", {
      "alias": {
        "crypto": "crypto-js"
      }
    }]
  ]
}

[Hi folks, my apologies for the lack of attention here. I've added @defunctzombie to the project in the hopes he can help move this project forward.]

So... here are my thoughts. Feel free to push back.

I have resisted adding dependencies to this library because node-uuid needs to work in browsers, where code footprint impacts performance and usability. Currently node-uuid is ~1.7K minified & compressed. Adding a crypto-js dependency would increase that by more than 10X. (FWIW, it's for this same reason I've resisted adding the code needed to implement MD5 and SHA hashing that would allow for v3() and v5() support.)

Also, I would argue that the lack of crypto support in react-native shouldn't be this project's problem. crypto is a standard api that a lot of libraries are likely to need. IMHO, it'd be better to get react-native to implement crypto support, rather than patching other modules on a one-off basis.

Project has been combined with uuid module project. If issue persists on latest version of uuid module please re-open with example and thoughts on how to fix.

This is the responsibility of react native (or some transpiling layer) to provide the expected API surface. This package already provides a hint in the package.json:browser field that a separate file can be used in browser(like) environments to serve as the RNG api.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nilfalse picture nilfalse  路  10Comments

guybedford picture guybedford  路  9Comments

josias-r picture josias-r  路  6Comments

sam-s4s picture sam-s4s  路  6Comments

Kmaschta picture Kmaschta  路  5Comments