alphapilgrim
alphapilgrim

Reputation: 3975

Node : SyntaxError: Unexpected token (

Getting the following error with this node module I'm messing around with. Any idea's on why the syntax error? After running the following command gets the error below:

node ./tester.js ./test.js
//test.js

var Test = (function () {

    add: function(num) {
        return num + num;
    };


 })();
if (module.exports) {
    module.exports = Test;
}

// tester.js

var testModule = process.argv[2],
    TestAdd = require(testModule);
console.log(TestAdd);

//OUTPUT 

    add: function(num) {
                 ^

SyntaxError: Unexpected token (

Upvotes: 2

Views: 559

Answers (1)

Abana Clara
Abana Clara

Reputation: 4650

This is a blatant syntax error. You must return the object.

var Test = (function () {
   return {
      add: function(num) {
          return num + num;
      }
   }   
})();

Or return the function

var Test = (function () {
   const add = function(num) {
       return num + num;
   }

   return add; 
})();

Upvotes: 5

Related Questions