beeth0ven beeth0ven - 1 month ago 31
Javascript Question

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

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:

enter image description here

Answer

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