版本兼容性问题Azure功能'Node和DirectlineJS es6导出

时间:2017-05-23 07:56:46

标签: node.js ecmascript-6 botframework azure-functions direct-line-botframework

结束目标:

使用DirectLine密钥创建运行Botframework-DirectlineJS并绑定到Bot (Framework)的azure函数。

问题:

上面提到的Botframework-DirectlineJS使用es6导出,Azure功能支持节点6.5.0 max doc。因此,问题如何成功导入Azure函数的index.js文件中的DirectlineJS?

Errror

```
2017-05-23T07:17:45.939 Exception while executing function: Functions.adapter. mscorlib: D:\home\site\wwwroot\adapter\importexportwrapper.js:1
(function (exports, require, module, __filename, __dirname) { import { DirectLine } from 'botframework-directlinejs';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:528:28)
    at Object.Module._extensions.(anonymous function) [as .js] (D:\home\site\wwwroot\node_modules\node-hook\index.js:73:14)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\home\site\wwwroot\adapter\index.js:4:2)
    at Module._compile (module.js:556:32).
```

目前错误是在尝试使用npm import-export

文件

  • index.js

    'use strict'; require('import-export'); require ('./importexportwrapper'); let directLine = new DirectLine({ secret: 'DirectlineSecretValue-here' } );

  • importexportwrapper.js

    import { DirectLine } from 'botframework-directlinejs';

2 个答案:

答案 0 :(得分:1)

不幸的是,似乎import-exportnode-hook无法与函数/ edgejs(我们用来运行节点的环境)配合良好。

有两种选择:

  • 使用babel将es6转换为es5作为部署过程的一部分。
  • 在typescript(index.ts)中编写你的函数,这将自动import转换 - 尽管这可能会因模块依赖而失败,我还没有尝试过

答案 1 :(得分:1)

您有三个选择:1)使用ES5编写代码,2)设置任务运行器(gulp / grunt / npm脚本)以使用Babel将ES6 +代码转换为ES5,或者3)在Typescript中编写代码并编译通过任务运行器/ npm脚本到ES5。

最直接的方法是:在您的文件importexportwrapper.js中使用require而不是import

示例:

var directline = require('botframework-directlinejs');

Babel + Gulp Option

安装:npm install --save-dev gulp gulp-babel

执行命令

var gulp = require("gulp");
var babel = require("gulp-babel");

gulp.task("default", function () {
  return gulp.src("src/app.js") // your source files
    .pipe(babel())
    .pipe(gulp.dest("dist")); // your compiled output directory
});

详细了解Azure功能Node.js versions here.