tradle / rn-nodeify

hack to allow react-native projects to use node core modules, and npm modules that use them
MIT License
619 stars 112 forks source link

Unable to resolve module `crypto` from `../node_modules/sjcl/sjcl.js` #44

Closed ghost closed 7 years ago

ghost commented 7 years ago

I keep getting this error after trying to run the app:

Unable to resolve module `crypto` from `../node_modules/sjcl/sjcl.js`: Module does not exist in the module map

Here's my postinstall script: rn-nodeify --install buffer,events,react-native-crypto,react-native-randombytes,stream,util,vm --hack

And here are the react-native and browser fields:

"react-native": {
  "_stream_duplex": "readable-stream/duplex",
  "_stream_passthrough": "readable-stream/passthrough",
  "_stream_readable": "readable-stream/readable",
  "_stream_transform": "readable-stream/transform",
  "_stream_writable": "readable-stream/writable",
  "crypto": "react-native-crypto",
  "stream": "stream-browserify",
  "vm": "vm-browserify"
},
"browser": {
  "_stream_duplex": "readable-stream/duplex",
  "_stream_passthrough": "readable-stream/passthrough",
  "_stream_readable": "readable-stream/readable",
  "_stream_transform": "readable-stream/transform",
  "_stream_writable": "readable-stream/writable",
  "crypto": "react-native-crypto",
  "stream": "stream-browserify",
  "vm": "vm-browserify"
}

Here's the shim.js generated:

if (typeof __dirname === 'undefined') global.__dirname = '/'
if (typeof __filename === 'undefined') global.__filename = ''
if (typeof process === 'undefined') {
  global.process = require('process')
} else {
  var bProcess = require('process')
  for (var p in bProcess) {
    if (!(p in process)) {
      process[p] = bProcess[p]
    }
  }
}

process.browser = false
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer

// global.location = global.location || { port: 80 }
var isDev = typeof __DEV__ === 'boolean' && __DEV__
process.env['NODE_ENV'] = isDev ? 'development' : 'production'
if (typeof localStorage !== 'undefined') {
  localStorage.debug = isDev ? '*' : ''
}

It might also be worth noting that the "crypto": "react-native-crypto" entries on the react-native & browser fields on package.json weren't generated automatically. I had to manually add them.

The weird thing is that it worked a couple of times (twice, to be exact). But when my modules changed and I had to npm install again it just won't run anymore. I wonder what's wrong. Any help would be appreciated!

rn-nodeify version: 7.0.1 npm version: 5.3.0 react-native version: 0.46.3

mvayngrib commented 7 years ago

@purrado make sure to use the names of the node core modules, not the shims themselves, i.e.: rn-nodeify --install ...,crypto, and NOT rn-nodeify --install ...,react-native-crypto,

ghost commented 7 years ago

@mvayngrib That did the trick! I remember trying it for randombytes but it threw an error (allShims[name] is undefined) so I just assumed it wouldn't work for crypto. Looked carelessly through the code and found allShims are imported from ./shims.js so I just assumed that the entries there would work. I should have looked at ./coreList.js, I think. Anyway, thank you so much! 😄

mvayngrib commented 7 years ago

@purrado you're very welcome, glad you got it working :)