3

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

beeth0ven
  • 1,857
  • 15
  • 18
  • The OP updated the original question, and (rightly) added that the arrow function was not the culprit. My mistake, I gave a wrong answer below and thereby misled the community into marking the question as duplicate. I think this question should be reopened. – Manube Oct 30 '16 at 17:24

1 Answers1

4

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');
}
Community
  • 1
  • 1
Manube
  • 5,110
  • 3
  • 35
  • 59
  • Thanks for rapid response , but sorry this may not help. After change to `function(event, context, callback) {...}` , Same error throw again. – beeth0ven Oct 30 '16 at 16:41
  • @beeth0ven you are right, it seems arrow functions are available in AWS Nodejs 4.3: https://aws.amazon.com/blogs/compute/node-js-4-3-2-runtime-now-available-on-lambda/ My answer is therefore wrong, and your question may not be a duplicate; I would delete my answer, but then these comments would be gone, wouldn't they? – Manube Oct 30 '16 at 17:16
  • After test code on node v5.1.1, It's throws error `SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode`, So I add `'use strict'` to file event.js. Then it works.Thanks for giving me the important information. Could you please update answer to: " just add use strict to file event.js`. And I will mark your answer as correct.Thanks. – beeth0ven Oct 31 '16 at 03:15
  • Glad the issue is fixed :) – Manube Oct 31 '16 at 11:06