AWS Lambda exports class 在 node.js v6.4 中有效,但在 node.js v4.3 中无效,如何解决这个问题?

AWS Lambda exports class works in node.js v6.4 but not in node.js v4.3, how to fix this?

我的代码在 node.js v6.4 中有效: 只有两个文件,index.js:

  // ------------ Index.js ------------ 
  'use strict';

  var Event = require('./models/event.js');

  exports.handler = (event, context, callback) => {
     console.log('done');
  }

和event.js:

  // ------------ Event.js ------------ 

  class Event {
    static get dynamoDBTableName() {
      return
    }
    get hashValue() {
      return
    }
    parseReference(reference) {
      return
    }
  }

  exports.Event = Event

当 运行 index.handler 在使用版本 node.js 4.3 的 AWS Lambda 上时,它会抛出一个错误:

  Syntax error in module 'index': SyntaxError
  at exports.runInThisContext (vm.js:53:16)
  at Module._compile (module.js:373:25)
  at Object.Module._extensions..js (module.js:416:10)
  at Module.load (module.js:343:32)
  at Function.Module._load (module.js:300:12)
  at Module.require (module.js:353:17)
  at require (internal/module.js:12:17)
  at Object.<anonymous> (/var/task/index.js:16:13)
  at Module._compile (module.js:409:26)
  at Object.Module._extensions..js (module.js:416:10)

我认为 exports.Event = Event

有问题

有没有什么技巧可以解决这个问题。

我是 node.js 的新手。

如有任何帮助,我们将不胜感激。

我认为这不是 (event, context, callback) => { }

的 SyntaxError

因为 AWS Lambda 示例代码运行适用于此语法:

我原本以为箭头函数是罪魁祸首。但是,AWS Node.js 4.3.2 确实支持箭头功能,如 post about Node.js 4.3.2 Runtime on Lambda.

中所述

新(正确)答案

event.js 文件是否以 'use strict'; 开头?

node.js 4.3.2

中的 class 声明必须使用严格模式

Mozilla Developer Network about strict mode

希望这会有所帮助...


原始(不正确)答案

module.exports = 产品

我相信箭头函数:

() => {}

尚未在您使用的 nodejs 版本 (4.3) 中实现。

See this answer

Node.js 自版本 4.4.5

支持箭头函数

如果更新您的 nodejs 版本不适合您,您可以替换:

  exports.handler = (event, context, callback) => {
    console.log('done');
  }

  exports.handler = (event, context, callback) = function() {
     console.log('done');
}