Twilio React Native - Unable to resolve module crypto

限于喜欢 提交于 2019-12-12 07:57:07

问题


I'm working on implementing the twilio package into my react-native project and when I require it in my file the project wont load and I'm seeing the following error:

Unable to resolve module crypto from /Users/[myname]/Documents/Projects/React-Native/[app-name]/node_modules/twilio/lib/webhooks.js: Unable to find this module in its module map or any of the node_modules directories under /Users/node_modules/crypto and its parent directories

I've tried installing the crypto package directly and that doesn't seem to work either.

Has anyone experienced this issue, and has a way to resolve it?


回答1:


You can use the rn-nodeify module to get crypto on react-native.

Add rn-nodeify to your devDependencies in package.json:

"devDependencies": {
  "rn-nodeify": "^6.0.1"
}

Add the following to the scripts section of the same file:

"scripts": {
  …
  "postinstall": "node_modules/.bin/rn-nodeify --install crypto --hack"
}

Be aware that rn-nodeify will modify your package.json.

More information available here: https://www.npmjs.com/package/rn-nodeify




回答2:


It seems that React Native doesn't accept certain packages based on their dependencies, Twilio being one of these.

While not a direct solution, I created a work around to this issue by creating a separate Express server to make the Twilio call, and calling that route from within my React Native app.




回答3:


I suggest you have a look there, plenty of solutions are given because none seem to fix for everyone.

I suggest you try the following (taken from the issue from the link) :

  1. rm -rf node_modules
  2. rm -fr $TMPDIR/react-*
  3. watchman watch-del-all
  4. npm cache clean && npm install
  5. npm start from ./node_modules/react-native

But check out the issue in its integrality, many found other fixes that worked for them.




回答4:


React Native packager uses Babel under the hood. This means that you can use babel-plugin-rewrite-require Babel plugin to rewrite all require('crypto') calls to require('crypto-browserify'), assuming that the latter is installed in your node_modules.

As of January 2016, you can use .babelrc file to define optional configuration, so this becomes really easy. First, install the dependencies:

npm install --save crypto-browserify
npm install --save-dev babel-plugin-rewrite-require

Then add plugins config to your .babelrc file:

{
  "presets": ["react-native"],
  "plugins": [
    ["babel-plugin-rewrite-require", {
      aliases: {
        crypto: 'crypto-browserify'
      }
    }]
  ]
}

Restart the packager and that should be it.

This is the same approach that ReactNativify uses, except that here we use .babelrc instead of defining custom transformer. When ReactNativify was written, it was not supported, so they had to go with more complex solution. See this file from ReactNativify for almost complete list of node polyfills.



来源:https://stackoverflow.com/questions/36415157/twilio-react-native-unable-to-resolve-module-crypto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!