我在ES6上有一些项目。对于一个文件的exaple:
export default function (a, b)
{
return a+b;
}
我使用webpack和babel将其转换为一个带有ES2015代码的文件。得到这样的东西:
/******/ (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;
};
/***/ }
/******/ ]);
现在我想在Azure功能中使用我的功能。为此我创建文件calc.js并用webpack输出填充它并尝试从我的main函数调用它:
var calc = require('./calc');
module.exports = function (context, req) {
var result = calc(2);
context.done(null, result);
};
并收到错误
执行函数时出现异常:Functions.HttpTriggerJS1。 mscorlib:TypeError:calc不是函数 在module.exports(D:\ home \ site \ wwwroot \ HttpTriggerJS1 \ index.js:11:30) 在D:\ Program Files(x86)\ SiteExtensions \ Functions \ 1.0.10690 \ bin \ Content \ Script \ functions.js:83:24。
现在Questin:我应该如何准备我的代码以在Azure Function中调用它?
答案 0 :(得分:3)
基本上,您不需要将代码转换为ES5,因为在Azure Functions中运行的代码是服务器端的Node.js Javascript而不是前端Javascript。最新的Node.js支持大多数ES6(ES2015)语法和功能。
网站node.green提供了一个很好的概述 在各种版本的Node.js中支持ECMAScript功能,基于 kangax的compat-table。
所以请考虑将Node / NPM的版本升级到更高版本。要做到这一点,请参阅下文。
您可以在Node WebApp的URL
https://<yourappname>.scm.azurewebsites.net/api/diagnostics/runtime
上查看Azure支持的所有节点版本。
希望它有所帮助。
答案 1 :(得分:2)
对于大多数网络应用程序,Aaron是正确的,WEBSITE_NODE_DEFAULT_VERSION
将设置应用程序将运行的节点版本。
不幸的是,azure函数不会以相同的方式运行节点,并且由于对edgejs的依赖而被锁定在6.5.0。
其中一个github问题对于如何启用捆绑azure函数有一个很好的comment。在您的具体情况下,我认为您需要在包本身中包含函数的require
,而不是简单地导出函数:
global.deps = { calc: require('./calc') };
然后,在你的函数中使用bundle:
// 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));
}