简体   繁体   English

节点:SyntaxError:意外令牌(

[英]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 (

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM