NodeJs:TypeError:require(...)不是函数 [英] NodeJs : TypeError: require(...) is not a function

查看:5776
本文介绍了NodeJs:TypeError:require(...)不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试要求一个文件,然后将其传递给var。我正在关注这个教程来创建一个身份验证系统。在编写server.js文件并尝试编译后,我得到了一个bson错误,因此我更改了需要在mongoose中发布版本的行。

I am trying to require a file and afterwards pass it to a var. I am following this tutorial to create a authentication system. After writing the server.js file and trying to compile I got a bson error therefore I changed the line that required the release version of it in mongoose.

这是我的代码和错误:

server.js

server.js

    require('./app/routes')(app, passport);

错误

Error

require('./app/routes')(app, passport);
                   ^

TypeError: require(...) is not a function
           at Object.<anonymous> (d:\Node JS learning\WorkWarV2\server.js:38:24)
           at Module._compile (module.js:434:26)
           at Object.Module._extensions..js (module.js:452:10)
           at Module.load (module.js:355:32)
           at Function.Module._load (module.js:310:12)
           at Function.Module.runMain (module.js:475:10)
           at startup (node.js:117:18)
           at node.js:951:3

Process finished with exit code 1

我读过这通常意味着requireJS尚未正确加载我不知道为什么或如何修复它。

I have read that this usually means that requireJS is not getting loaded properly yet I am not aware why or how to fix it.

由于评论而编辑:

Edit due to comment:

根据要求,此处 console.log的结果( require);

推荐答案

我认为这意味着 module.exports <您的 ./ app / routes 模块中的/ code>未被指定为函数因此 require('。/ app / routes')没有解析为函数因此,你不能把它称为像这样的函数要求('./ app / routes')(app,passport)

I think this means that module.exports in your ./app/routes module is not assigned to be a function so therefore require('./app/routes') does not resolve to a function so therefore, you cannot call it as a function like this require('./app/routes')(app, passport).

如果您希望我们对此进行进一步评论,请告诉我们 ./ app / routes

Show us ./app/routes if you want us to comment further on that.

看起来应该是这样的;

It should look something like this;

module.exports = function(app, passport) {
    // code here
}

您正在导出一个函数然后可以像 require('./ app / routes')(app,passport)一样调用。

You are exporting a function that can then be called like require('./app/routes')(app, passport).

发生类似错误的另一个原因是,如果你有一个循环模块依赖,模块A试图 require(B)并且模块B正在尝试 require(A)。发生这种情况时,它将被 require()子系统检测到,其中一个将返回 null 因此试图将其称为函数将无法正常工作。在这种情况下,修复是删除循环依赖,通常是将公共代码分解为第三个模块,这两个模块都可以单独加载,但修复循环依赖的细节对于每种情况都是唯一的。

One other reason a similar error could occur is if you have a circular module dependency where module A is trying to require(B) and module B is trying to require(A). When this happens, it will be detected by the require() sub-system and one of them will come back as null and thus trying to call that as a function will not work. The fix in that case is to remove the circular dependency, usually by breaking common code into a third module that both can separately load though the specifics of fixing a circular dependency are unique for each situation.

这篇关于NodeJs:TypeError:require(...)不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆