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.


  1. npm install -g browserify
  2.  Create a file crypto-in.js that imports the crypto module and simply exports it: 
    var crypto=require("crypto");
    module.exports=crypto;
  3.  Create a standalone Javascript bundle crypto.js using browserify in NPM commandline window.
    browserify crypto-in.js -o crypto.js
  4.   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;
  5. 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 quality
    if ('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.

Comments


  1. We 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

    ReplyDelete

  2. We 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Top JavaScript courses helping you develop SPFx webPart with ReactJS and AngularJS

SharePoint 2013 error - "Application error when access /_vti_bin/client.svc"

Enable the Microsoft Power BI report file type (.pbix) in SharePoint Search