【问题标题】:I am getting x is not a function in javascript我得到 x 不是 javascript 中的函数
【发布时间】:2019-04-06 14:01:42
【问题描述】:

我写了一个简单的程序,其中写了一个 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

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 效果很好,谢谢卡里姆,过去两个小时我都被这个问题困住了
  • 太好了,很高兴它有帮助:)
猜你喜欢
  • 2021-12-02
  • 1970-01-01
  • 2022-01-02
  • 2021-12-13
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
相关资源
最近更新 更多