版本兼容性问题Azure函数的Node和DirectlineJS es6导出 [英] Version compatibility issue Azure functions' Node and DirectlineJS es6 exports
问题描述
要创建运行 Botframework-DirectlineJS 的Azure函数,并使用DirectLine秘密绑定到Bot (Framework)
To create Azure Function running Botframework-DirectlineJS with binding to Bot (Framework)
using DirectLine secret.
上述Botframework-DirectlineJS使用ES6导出和Azure函数支持Node 6.5.0 max
The above mentioned Botframework-DirectlineJS uses ES6 export and Azure Functions support Node 6.5.0 max doc. Hence the question how to import successfully the DirectlineJS in the index.js file of Azure function?
```
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
Currently the error was while trying to use npm import-export
-
index.js
index.js
require('import-export');
require ('./importexportwrapper');
let directLine = new DirectLine({
secret: 'DirectlineSecretValue-here'
}
);```
importexportwrapper.js
importexportwrapper.js
import { DirectLine } from 'botframework-directlinejs';
推荐答案
您有三个选择:1)使用ES5编写代码,2)设置任务运行程序(gulp/grunt/npm脚本),将ES6 +代码转换为使用Babel的ES5,或3)用Typescript编写代码,然后通过任务运行程序/npm脚本将其编译为ES5.
You have three options: 1) Write your code using ES5, 2) setup a task runner (gulp/grunt/npm scripts) to convert your ES6+ code to ES5 using Babel, or 3) write your code in Typescript and compile it to ES5 via task runner/npm scripts.
最直接的方法是:在文件importexportwrapper.js
中使用require
而不是import
.
The most straight-forward method would be: in your file importexportwrapper.js
use require
instead of import
.
示例:
var directline = require('botframework-directlinejs');
安装: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版本.
Read more about Azure Functions Node.js versions here.
这篇关于版本兼容性问题Azure函数的Node和DirectlineJS es6导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!