Javascript NodeJs:TypeError:require(...) 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33007878/
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 14:30:21  来源:igfitidea点击:

NodeJs : TypeError: require(...) is not a function

javascriptnode.jsrequire

提问by taigi100

I am trying to require a file and afterwards pass it to a var. I am following thistutorial 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.

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

Here are my code and error :

这是我的代码和错误:

server.js

服务器.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

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.

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

Edit due to comment:

编辑由于评论:

As asked, hereis the result of console.log(require);

正如所问,是结果console.log(require);

回答by jfriend00

I think this means that module.exportsin your ./app/routesmodule 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).

我认为这意味着module.exports在您的./app/routes模块中未分配为函数,因此require('./app/routes')不会解析为函数,因此,您不能将其称为这样的函数require('./app/routes')(app, passport)

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

让我们看看./app/routes,如果你需要我们进一步评论说。

It should look something like this;

它应该看起来像这样;

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

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

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



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 nulland 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.

可能发生类似错误的另一个原因是,如果您有一个循环模块依赖关系,其中模块 A 正在尝试,require(B)而模块 B 正在尝试require(A)。发生这种情况时,require()子系统会检测到它,其中一个会返回null,因此尝试将其作为函数调用是行不通的。在这种情况下,解决方法是消除循环依赖,通常是将公共代码分解为第三个模块,这两个模块都可以单独加载,尽管修复循环依赖的细节对于每种情况都是独一无二的。

回答by mCY

For me, when I do Immediately invoked function, I need to put ;at the end of require().

对我来说,当我立即调用函数,我需要把;在年底require()

Error:

错误:

const fs = require('fs')

(() => {
  console.log('wow')
})()

Good:

好的:

const fs = require('fs');

(() => {
  console.log('wow')
})()

回答by Bryan Larsen

For me, this was an issue with cyclic dependencies.

对我来说,这是循环依赖的问题。

IOW, module A required module B, and module B required module A.

IOW,模块A需要模块B,模块B需要模块A。

So in module B, require('./A')is an empty object rather than a function.

所以在模块 B 中,require('./A')是一个空对象而不是一个函数。

How to deal with cyclic dependencies in Node.js

如何处理 Node.js 中的循环依赖

回答by Kote Isaev

For me, I got similar error when switched between branches - one used newer ("typescriptish") version of @google-cloud/datastorepackages which returns object with Datastore constructor as one of properties of exported object and I switched to other branch for a task, an older datastore version was used there, which exports Datastore constructor "directly" as module.exportsvalue. I got the error because node_modules still had newer modules used by branch I switched from.

对我来说,在分支之间切换时我遇到了类似的错误 - 一个使用较新(“typescriptish”)版本的@google-cloud/datastore包,它返回带有 Datastore 构造函数的对象作为导出对象的属性之一,我切换到另一个分支进行任务,一个较旧的数据存储版本在那里使用,它“直接”导出数据存储构造函数作为module.exports值。我收到错误是因为 node_modules 仍然有我切换的分支使用的较新模块。

回答by mehmoodchohan

Just wrap in Arrow function where you requiring the files

只需在需要文件的地方包装 Arrow 函数

回答by Milani Igor

Remember to export your routes.js.

记得导出你的routes.js.

In routes.js, write your routes and all your code in this function module:

在 中routes.js,在此功能模块中编写您的路线和所有代码:

exports = function(app, passport) {

/* write here your code */ 

}