sepulka
sepulka

Reputation: 405

How to prepare my code for Azure Functions

I have some project on ES6. For exaple with one file:

export default function (a, b)
   {
    return a+b;
   }

I'm transform it using webpack and babel to one file with ES2015 code. And get something like this:

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};

/******/    // The require function
/******/    function __webpack_require__(moduleId) {

/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;

/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            exports: {},
/******/            id: moduleId,
/******/            loaded: false
/******/        };

/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/        // Flag the module as loaded
/******/        module.loaded = true;

/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }


/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;

/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;

/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "/";

/******/    // Load entry module and return exports
/******/    return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

    module.exports = __webpack_require__(1);


/***/ },
/* 1 */
/***/ function(module, exports) {

    "use strict";

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    exports.default = function (a, b) {
      // return aCalc(a) + bCalc(b);
      return a + b;
    };

/***/ }
/******/ ]);

Now I want use my function in Azure Function. For this I create file calc.js and fill it with webpack output and try to call it from my main function:

var calc = require('./calc');

module.exports = function (context, req) {
var result = calc(2);
context.done(null, result);
};

and get error

Exception while executing function: Functions.HttpTriggerJS1. mscorlib: TypeError: calc is not a function at module.exports (D:\home\site\wwwroot\HttpTriggerJS1\index.js:11:30) at D:\Program Files (x86)\SiteExtensions\Functions\1.0.10690\bin\Content\Script\functions.js:83:24.

And now Questin: how I should prepare my code for call it in Azure Function?

Upvotes: 1

Views: 2761

Answers (2)

Matt Mason
Matt Mason

Reputation: 2726

For most web apps Aaron is correct, WEBSITE_NODE_DEFAULT_VERSION will set the version of node that your application will run.

Unfortunately azure functions does not run node in the same way and is locked at 6.5.0 due to a dependency on edgejs.

One of the github issues has a good comment on how they enabled bundling for azure functions. In your specific case, I think that you will need to include a require of your function in the bundle itself, instead of simply exporting the function:

global.deps = { calc: require('./calc') };

Then, to use the bundle in your function:

// require your bundle
require('<nameofyourbundle>');

// dependencies are stored in the global object under 'deps'
var calc = global.deps.calc;

// your exported function
module.exports = (context, req) => {
  context.done(null, calc(2));
}

Upvotes: 2

Aaron Chen
Aaron Chen

Reputation: 9950

Basically, you do not need to transform your code to ES5, because the code run in Azure Functions is Node.js Javascript in server side not front-end Javascript. And the latest Node.js supports most of the ES6 (ES2015) syntax and features.

The website node.green provides an excellent overview over supported ECMAScript features in various versions of Node.js, based on kangax's compat-table.

So please consider upgrading version of Node/NPM to higher instead. To do this see below.

  1. Go to the Azure portal in your browser.
  2. Navigate to your Function App and click Function app settings.
  3. Click Configure app settings button, then set WEBSITE_NODE_DEFAULT_VERSION to 7.2.0. enter image description here

  4. After that done, you check that in dev console. enter image description here

You can see all node version supported by Azure at the URL https://<yourappname>.scm.azurewebsites.net/api/diagnostics/runtime of Node WebApp.

Hope it helps.

Upvotes: 3

Related Questions