Javascript Node.js 中的“TypeError: is not a function”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33865068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:34:34  来源:igfitidea点击:

'TypeError: is not a function' in Node.js

javascriptnode.js

提问by Karthick Gk

I'm getting the error while running the following code in Node.js

在 Node.js 中运行以下代码时出现错误

var assert = require('assert');
var request = require('request');
var index = require('./index');
it('verify javascript function', function(done) {
    var v2 = index.AddNumbers(5, 6);
    assert.equal(11, v2);
    done();
});

The index.jsfile contain the following code:

index.js文件包含以下代码:

function AddNumbers(a,b){
    return a+b;
}

What am I doing wrong?

我究竟做错了什么?

回答by shimi_tap

This happened to me many times because of circular dependency, check if you have 2 classes that are requiring each other, remove one of them from requiring the other and the issue should be solved

由于循环依赖,这种情况在我身上发生过很多次,检查您是否有 2 个相互需要的类,将其中一个从要求另一个中删除,问题应该得到解决

回答by T.J. Crowder

With NodeJS modules, to make something public, you have to export it. Add this to the end of index.js:

使用 NodeJS模块,要公开某些内容,您必须将其导出。将此添加到末尾index.js

module.exports.AddNumbers = AddNumbers;


Here it is running on my machine:

它在我的机器上运行:

$ cat index.js 
function AddNumbers(a,b){
    return a+b;
}

module.exports.AddNumbers = AddNumbers;

$ cat example.js 
var index = require('./index');
var v2 = index.AddNumbers(5,6);
console.log(v2);

$ node example.js
11

回答by Aravinda Meewalaarachchi

If you need to expose a specific component, function or a variable to public. You have to exportsthose components using JavaScript modules.

如果您需要向公共公开特定的组件、函数或变量。您必须exports使用 JavaScript 模块来处理这些组件。

let add = (a,b)=>{
    return ( a+b);
}
module.exports.add=add;

or if you want to expose multiple functions, you can do as follows.

或者如果你想暴露多个函数,你可以这样做。

let add = (a,b)=>{
    return (a+b);
}
let subtract = (a, b)=>{
    return (a-b);
}
module.exports={
   add : add,
   subtract : subtract
};

回答by Indunil

Your "AddNumbers" function in the "index.js" file should be as follows,

“index.js”文件中的“AddNumbers”函数应如下所示,

function AddNumbers(a,b){
    var addition = function(a, b){
      return (a + b) ;
    };

    module.exports = {
       additionResult: addition
    };
}

And you need to call it in your "Node.js" file as follows

你需要在你的“Node.js”文件中调用它,如下所示

var assert = require('assert');
var request = require('request');
var index = require('./index');
it('verify javascript function', function(done) {
    var v2 = index.additionResult(5, 6);
    assert.equal(11, v2);
    done();
});

This should work. Please note that you call the function by whatever the token name you exported the return value by (I use a different name here just for clarity). Almost everybody uses the same name as the function name so there are no confusion. Also in ES6, if you use the same name you can export as just,

这应该有效。请注意,您通过导出返回值的任何令牌名称调用该函数(为了清楚起见,我在这里使用了不同的名称)。几乎每个人都使用与函数名称相同的名称,因此不会混淆。同样在 ES6 中,如果您使用相同的名称,则可以导出为,

module.exports = {
       addition
};

instead of,

代替,

module.exports = {
       addition: addition
};

since you use the same name. It is an ES6 feature.

因为您使用相同的名称。这是一个 ES6 特性。

回答by Tanya Branagan

I'm fairly a beginner at Node JS so I managed to get this error by importing a function like so:

我是 Node JS 的初学者,所以我通过导入这样的函数设法得到了这个错误:

const { functionName } = require('./function')

instead of like so:

而不是这样:

const functionName = require('./function')