Resolve error :"Unable to resolve module `crypto` in \\azure-mobile-apps-client\\node_modules\\node-uuid\\uuid.js''
I met issue "Unable to resolve module `crypto` in \\azure-mobile-apps-client\\node_modules\\node-uuid\\uuid.js'" when I developed an App using React Native and Azure Mobile App Service.
The Azure Mobile App Service uses the crypto module in "uuid.js" such as "require('crypto').randomBytes", because crypto is a core Node JS module, and the React Native packager can’t package it along with app’s Javascript bundle, so it throws a runtime error: Unable to resolve module 'crypto'.
I did a research on google.com, and fount out this article "Using Core Node JS Modules in React Native Apps". Based on this article, I came out a solution to resolve this problem as below.
The Azure Mobile App Service uses the crypto module in "uuid.js" such as "require('crypto').randomBytes", because crypto is a core Node JS module, and the React Native packager can’t package it along with app’s Javascript bundle, so it throws a runtime error: Unable to resolve module 'crypto'.
I did a research on google.com, and fount out this article "Using Core Node JS Modules in React Native Apps". Based on this article, I came out a solution to resolve this problem as below.
- npm install -g browserify
-
Create a file
crypto-in.js
that imports thecrypto
module and simply exports it:var crypto=require("crypto");
module.exports=crypto; - Create a standalone Javascript bundle
crypto.js
usingbrowserify
in NPM commandline window.
browserify crypto-in.js -o crypto.js
- After the changes,
crypto.js
should look something like this:var crypto;
// Some 21,500 lines of polyfills..
},{"indexof":100}],153:[function(reqq,module,exports){
crypto = reqq("crypto");
},{"crypto":56}]},{},[153]);
module.exports = crypto; -
In “\\node_modules\\azure-mobile-
apps-client\\node_modules\\ node-uuid\\uuid.js”,update the code as below: function setupNode() {
// Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html// Moderately fast, high qualityif ('function' === typeof require) {try {//var _rb = require('crypto').randomBytes;var _rb =require('./crypto').randomBytes;
_nodeRNG = _rng = _rb && function() {return _rb(16);};_rng();} catch(e) {}}}
After I done that, the App can be compiled successfully.
ReplyDeleteWe Providing Mulesoft Training Courses In Hyderabad Ameerpet.Mulesoft
Training In Hyderabad. Mule ESB Training In Ameerpet Hyderabad.100% Job Oriented
Certifications Assistance.Placement Guaranteed Mulesoft Training Courses in Hyderabad
ReplyDeleteWe are providing best software institute in Hyderabad and best online training
in Hyderabad and all IT courses in Hyderabad.Expert trainers in JSV IT SOLUTIONS