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

烈酒焚心 提交于 2020-01-13 05:26:11

问题


I have code works in node.js v6.4: just two files, index.js:

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

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

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

and event.js:

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

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

  exports.Event = Event

when run index.handler on AWS Lambda which use version node.js 4.3, it throws a error:

  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)

I think it's something wrong with exports.Event = Event,

Is there some trick to fix this.

I'm new to node.js.

Any help should be appreciated.

I think it's not SyntaxError with (event, context, callback) => { }

Because AWS Lambda sample code runs well with this Syntax:


回答1:


I originally thought the arrow function was the culprit. However, AWS Node.js 4.3.2 DOES support the arrow function, as mentioned in this post about Node.js 4.3.2 Runtime on Lambda.


NEW (correct) ANSWER

Does the event.js file start with 'use strict';?

You must use strict mode for a class declaration in node.js 4.3.2

Mozilla Developer Network about strict mode

Hoping this will help...


ORIGINAL (incorrect) ANSWER

module.exports = Products

I believe the arrow function:

() => {}

is not yet implemented in the nodejs version you are using (4.3).

See this answer

Arrow functions are supported in Node.js since version 4.4.5


If updating your nodejs version is not an option for you, you could replace:

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

with

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


来源:https://stackoverflow.com/questions/40331059/aws-lambda-exports-class-works-in-node-js-v6-4-but-not-in-node-js-v4-3-how-to-f

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