javascript - 我得到 x is not a function in javascript

标签 javascript node.js command-line error-handling

我编写了一个简单的程序,其中编写了一个java脚本函数,如下所示

function add( num1,num2){
    return num1 + num2;
}


module.export={
    add
}

console.log (add(5,4));

下面给出的是另一个函数index.spec.js,它从index.js导入add函数

const assert = require('assert')
const { add } = require('./index')


console.log(add(5,6))

因此,当我尝试使用 npm commmand 运行 index.spec.js 时,我收到错误“add is not afunction”,如下所示 error

最佳答案

您导出模块的方式错误,请将其更改为

module.exports = { add }; //with the final s

关于javascript - 我得到 x is not a function in javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53116646/

相关文章:

javascript - 我不明白为什么这个 JS/JQuery IF 语句没有像我期望的那样工作

javascript - 如何防止单击按钮导致 Pane 折叠

javascript - 将 json 对象转为数组

asp.net - 从 javascript 函数调用 vb.net 子路由?

command-line - 在命令行上导出 weka 结果

windows - Par 段落格式化程序 - 如何在 Windows 命令行中使用它

node.js - 连接 NodeMCU Lua 套接字客户端与 node.js socket.io 服务器

node.js - 使用我的应用程序登录登录我的 Gitlab CE 安装

javascript - 数组和 ArrayBuffer 有什么区别?

linux - 将文件的输出一行发送到远程 Linux 机器