javascript - 节点:语法错误:意外的令牌(
问题描述
我正在搞砸的这个节点模块出现以下错误。关于为什么语法错误的任何想法?运行以下命令后出现以下错误:
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 (
解决方案
这是一个明显的语法错误。您必须返回对象。
var Test = (function () {
return {
add: function(num) {
return num + num;
}
}
})();
或者返回函数
var Test = (function () {
const add = function(num) {
return num + num;
}
return add;
})();
推荐阅读
- java - HexBinary字节顺序
- javascript - JQuery datatablejs土耳其字符搜索问题
- sql - 使用左连接 SQL Server 时如何提高搜索速度?
- android - 为什么 button.ispressed() 不适用于 android 的东西
- javascript - (修改 JSFiddle)如何根据上一个下拉列表中选择的内容制作附加级别下拉列表?
- javascript - 有没有办法按照 CloudFormation 模板中指定的顺序获取 ProvisioningArtifactParameters?
- sql-server - 在 SQL Server 2017 中注册引用其他程序集的 CLR 程序集时出错
- python - 从列表列表中删除重复项
- php - PHP重新排序包含重复项目的数组以在重复项目之间建立/保持距离
- mysql - 无法添加外键约束 errno: 1215 sequlize/mysql