Version compatibility issue Azure functions' Node and DirectlineJS es6 exports

有些话、适合烂在心里 提交于 2019-12-08 01:48:28

问题


End Goal:

To create Azure Function running Botframework-DirectlineJS with binding to Bot (Framework) using DirectLine secret.

Issue:

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?

Error

```
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).
```

Currently the error was while trying to use npm import-export

Files

  • index.js

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

    import { DirectLine } from 'botframework-directlinejs';


回答1:


Unfortunately it seems like import-export or node-hook doesn't play well with functions / edgejs (the environment we use to run node).

A couple options to try:

  • use babel to transpile es6 to es5 as a part of your deployment process.
  • write your function in typescript (index.ts) which will do import transpilation automatically - though this may fail for module dependencies, I haven't tried it out



回答2:


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.

The most straight-forward method would be: in your file importexportwrapper.js use require instead of import.

Example:

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

Babel + Gulp Option

Install: npm install --save-dev gulp gulp-babel

Run:

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
});

Read more about Azure Functions Node.js versions here.



来源:https://stackoverflow.com/questions/44128985/version-compatibility-issue-azure-functions-node-and-directlinejs-es6-exports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!