none
[Node.js][Mobile Apps] Unaught exception: Error: Cannot find module 'azure' - var azure = require('azure-storage'); RRS feed

  • Question

  • This is simply not working. I am trying to use Azure Mobile Services with a custom API and Node.js backend to generate a Shared Access Signature.  I used the console to install azure-storage, but I keep getting this error every time the script runs. This is so frustrating! Here is my script.

    exports.get = function(request, response) {
        //var azure = require('azure');
        
        var azure = require('azure-storage');
    
        var qs = require('querystring');
        var appSettings = require('mobileservice-config').appSettings;
        
        var accountName = appSettings.AZURE_STORAGE_ACCOUNT;
        var accountKey = appSettings.AZURE_STORAGE_ACCESS_KEY;
        var host = accountName + '.blob.core.windows.net';
        
        // The first 2 parameters are being drawn from the config settings of AMS
        var blobSvc = azure.createBlobService(accountName, accountKey, host);
        
        var startDate = new Date();
        var endDate = new Date(startDate);
        
        endDate.setMinutes(startDate.getMinutes() + 5);
        
        var sharedAccessPolicy = {
            AccessPolicy: {
            Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
            Start: startDate,
            Expiry: endDate
        }
        };
                
        // Blob name is optional here
        var sharedAccessKey = blobSvc.generateSharedAccessSignature(request.query.container.toLowerCase(), '', sharedAccessPolicy);
      
        var sasQueryString = qs.stringify(sharedAccessKey.queryString);
        
        response.send(statusCodes.OK, JSON.stringify(sasQueryString)); 
    };
     

    Any help is much appreciated. This entire install is a couple of weeks old, so perhaps this is a new bug I've found.

    Thanks!




    Tuesday, January 12, 2016 3:44 AM

Answers

  • According your question, you host your server on Mobile Service, not Mobile Apps. Actually, Azure Mobile Service and Azure Mobile Apps both are backend servers for mobility applications, but they are a bit difference in architecture. You can find there 2 servers in Azure new portal.

    Mobile App is hosted on Azure App Service platform, you can refer to https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-value-prop-migration-from-mobile-services/ for the information of the features.

    About Mobile Service in Node.js, you can refer to https://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#custom-api for more information.

    Wednesday, January 13, 2016 2:50 AM
    Moderator

All replies

  • If the entire install is only a couple of weeks old, then I would recommend you transition your project to Azure App Service Mobile Apps.  The App Service uses a completely standard ExpressJS-based system and has full support for npm package install.  You can also run the server locally to ensure it works.  You will need to use the Azure Storage Emulator to get that part.

    If this sounds like an idea, I'd recommend the following:

    Node.js Server SDK Docs:  https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/

    You can also use a Yeoman generator to generate a template project:

    npm install -g yo generator-azure-mobile-apps    
    mkdir myproject     
    cd myproject    
    yo azure-mobile-apps

    You can then use `npm install --save azure-storage` for deployment.  In terms of deployment options, you can upload all the files via ftp, deploy the files via git or link your app service to github or another source code control system.  Check out https://azure.microsoft.com/en-us/documentation/articles/web-sites-deploy/

    I also wrote a blog post on doing a user preferences store using Azure Table Storage in Azure App Service - you might want to check that out: http://shellmonger.com/2015/10/09/implementing-a-user-preferences-service-in-nodejs-with-azure-table-storage/

    Tuesday, January 12, 2016 4:00 AM
  • Adrian, thanks for the quick reply. It is an Azure App Service Mobile App. I used the term Azure Mobile Services because every tutorial I followed or read over the last couple of weeks defines it as such. :)  

    Here is my packages.json file.  I added "azure-storage" using the console (Visual Studio Online "Monaco") and this command.

    npm install 'azure-storage` --save 

    The --save switch adds it to the packages.json file.

    {
      "engines": {
        "node": "=4.2.1"
      },
      "name": "azure-mobile-apps.samples",
      "version": "0.0.0",
      "description": "Samples for Azure Mobile Apps",
      "author": "Microsoft",
      "dependencies": {
        "azure-mobile-apps": "^2.0.0-beta1",
        "azure-storage": "^0.7.0",
        "express": "^4.13.0"
      },
      "devDependencies": {}
    }

    I've restarted the mobile app several times. I keep getting the same error.

    Any other ideas?

    Thanks!


    • Edited by Dumber_Texan2 Tuesday, January 12, 2016 4:44 AM Added the --save explanation
    Tuesday, January 12, 2016 4:41 AM
  • Adrian, has this issue been  fixed?

    https://github.com/Azure/azure-storage-node/issues/39

    Caret (^) versions in package.json breaks older npm (node 0.8 and 0.6) #4849

    https://github.com/npm/npm/issues/4849 

    Tuesday, January 12, 2016 5:03 PM
  • Console: npm -v

    Didn't give the specific version.

    Console: npm install npm -g

    Updating to the latest version of NPM.

    npm http GET https://registry.npmjs.org/npm
    npm http 200 https://registry.npmjs.org/npm
    npm http GET https://registry.npmjs.org/npm/-/npm-3.5.3.tgz
    npm http 200 https://registry.npmjs.org/npm/-/npm-3.5.3.tgz
    D:\home\data\monaco\home\npm\npm -> D:\home\data\monaco\home\npm\node_modules\npm\bin\npm-cli.js
    npm@3.5.3 D:\home\data\monaco\home\npm\node_modules\npm

    Console: npm -v

    I am now running 1.4.10.

    I am now going to reinstall the Azure Storage SDK to see if that works.

     


    Tuesday, January 12, 2016 5:16 PM
  • Still doesn't work. Here is the Output.

    2016-01-12T17:47:08.028Z - error: Unable to load D:\home\site\wwwroot\api\myapp.js Error: Cannot find module 'azure'
        at Function.Module._resolveFilename (module.js:337:15)
        at Function.Module._load (module.js:287:25)
        at Module.require (module.js:366:17)
        at require (module.js:385:17)
        at Object.<anonymous> (D:\home\site\wwwroot\api\myapp.js:1:75)
        at Module._compile (module.js:435:26)
        at Object.Module._extensions..js (module.js:442:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:311:12)
        at Module.require (module.js:366:17)
        at require (module.js:385:17)
    Application has thrown an uncaught exception and is terminated:
    Error: Cannot find module 'azure'
        at Function.Module._resolveFilename (module.js:337:15)
        at Function.Module._load (module.js:287:25)
        at Module.require (module.js:366:17)
        at require (module.js:385:17)
        at Object.<anonymous> (D:\home\site\wwwroot\api\myapp.js:1:75)
        at Module._compile (module.js:435:26)
        at Object.Module._extensions..js (module.js:442:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:311:12)
        at Module.require (module.js:366:17)
        at require (module.js:385:17)



    Tuesday, January 12, 2016 5:50 PM
  • OK, I've gone ahead and installed the azure npm package. This seems to work, but now I'm getting this error. I'm now using require('azure') in my script.

          var azure = require('azure');
        
        //var azure = require('azure-storage');

    [info] Output will appear here from the running application at http://mobileimages.azurewebsites.net
    [info] The logging level is '8'. To change this, configure your application from https://manage.windowsazure.com/#Workspaces/WebsiteExtension/Website/mobileimages/configure
    2016-01-12T20:16:20.451Z - error:  Error: Cannot create Shared Access Signature unless the Account Name and Key are used to create the ServiceClient.
        at BlobService.generateSharedAccessSignatureWithVersion (D:\home\site\wwwroot\node_modules\azure\node_modules\azure-storage\lib\services\blob\blobservice.js:2138:11)
        at BlobService.generateSharedAccessSignature (D:\home\site\wwwroot\node_modules\azure\node_modules\azure-storage\lib\services\blob\blobservice.js:2112:15)
        at exports.get (D:\home\site\wwwroot\api\myapp.js:30:35)
        at Layer.handle [as handle_request] (D:\home\site\wwwroot\node_modules\express\lib\router\layer.js:95:5)
        at next (D:\home\site\wwwroot\node_modules\express\lib\router\route.js:131:13)
        at Route.dispatch (D:\home\site\wwwroot\node_modules\express\lib\router\route.js:112:3)
        at Layer.handle [as handle_request] (D:\home\site\wwwroot\node_modules\express\lib\router\layer.js:95:5)
        at D:\home\site\wwwroot\node_modules\express\lib\router\index.js:277:22
        at Function.process_params (D:\home\site\wwwroot\node_modules\express\lib\router\index.js:330:12)
        at next (D:\home\site\wwwroot\node_modules\express\lib\router\index.js:271:10)

    I guess I will have to fiddle with the correct combination of the Account name and Key. I know the Account name and Key is correct, so I assume there is some trick in getting this all to work correctly.


    Tuesday, January 12, 2016 8:22 PM
  • OK, I went ahead and hard coded the Account name and Key and here is the new error. I also commented out this section of code.

    //var appSettings = require('mobileservice-config').appSettings;

    ReferenceError: statusCodes is not defined

    I'm now going to research this error. I'm pretty much documenting all this under the same question, so it might help someone else. I will have a record of it as well.

    Tuesday, January 12, 2016 8:41 PM
  • I believe I fixed this error with the following code. I added the following line.

    var mobileApp = require('azure-mobile-apps');

    I then changed this line to use mobileApp.

    response.send(mobileApp.statusCodes, JSON.stringify(sasQueryString)); 

    I'm now receiving this error, which I will now research and see if I can fix.

    GMT express deprecated res.send(status, body): Use res.status(status).send(body) instead at api\myapp.js:36:14
    2016-01-12T20:52:17.256Z - error:  TypeError: Cannot read property 'toString' of undefined

    • Proposed as answer by leydar Monday, October 24, 2016 10:17 AM
    Tuesday, January 12, 2016 8:57 PM
  • OK, this code executes, but it returns a token with no value. The tokens value in the Watch window is "".

    response.status(200).send(JSON.stringify(sasQueryString));

    Here is the error message.

    System.ArgumentExceptionThe argument must not be empty string. Parameter name: sasToken


    Tuesday, January 12, 2016 10:12 PM
  • Here is a code sample in Mobile Service custom API, for you information:

    exports.get = function(request, response) {
        var azure = require('azure');
        var qs = require('querystring');
        var accountName = '<accountname>';
        var accountKey = '<accountkey>';
        var host = accountName + '.blob.core.windows.net';
        var blobService = azure.createBlobService(accountName, accountKey, host);
        
        var startDate = new Date();
        var expiryDate = new Date(startDate);
        expiryDate.setMinutes(startDate.getMinutes() + 30);
        startDate.setMinutes(startDate.getMinutes() - 30);
        
        var sharedAccessPolicy = {
          AccessPolicy: {
            Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
            Start: startDate,
            Expiry: expiryDate
          },
        };
        var blobSAS = blobService.generateSharedAccessSignature('mycontainer', 'myblob', sharedAccessPolicy);
        var sasQueryString = qs.stringify(blobSAS.queryString);
        var sasUri = blobSAS.baseUrl + blobSAS.path;
        response.send(sasUri+"?"+sasQueryString);
    };
    The "Azure" module is automatically created when mobile service created

    Wednesday, January 13, 2016 1:49 AM
    Moderator
  • Gary, thanks for that. By the way, I had to install 'azure', so I'm not sure what's wrong with the Azure App Service Mobile App Quick Start guide for Xamarin.Forms. I had to install a bunch of npm packages that were missing.
    Wednesday, January 13, 2016 2:36 AM
  • According your question, you host your server on Mobile Service, not Mobile Apps. Actually, Azure Mobile Service and Azure Mobile Apps both are backend servers for mobility applications, but they are a bit difference in architecture. You can find there 2 servers in Azure new portal.

    Mobile App is hosted on Azure App Service platform, you can refer to https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-value-prop-migration-from-mobile-services/ for the information of the features.

    About Mobile Service in Node.js, you can refer to https://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#custom-api for more information.

    Wednesday, January 13, 2016 2:50 AM
    Moderator