[英]NodeJs : TypeError: require(...) is not a function
I am trying to require a file and afterwards pass it to a var.我正在尝试需要一个文件,然后将其传递给 var。 I am following this tutorial to create an 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 文件并尝试编译后,我遇到了 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, here is the result of console.log(require);
正如所问,这是
console.log(require);
的结果
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)
.我认为,这意味着
module.exports
在./app/routes
模块没有分配是一个函数等于是require('./app/routes')
不能解决的功能,所以因此,你不能把它作为一个像这样的功能require('./app/routes')(app, passport)
。
Show us ./app/routes
if 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)
.可能发生类似错误的另一个原因是,如果您有一个循环模块依赖关系,其中模块 A 尝试
require(B)
而模块 B 尝试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.当这种情况发生时,它会被
require()
子系统检测到,其中一个将返回为null
,因此尝试将其作为函数调用将不起作用。 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.在这种情况下,解决方法是消除循环依赖,通常是将公共代码分解为第三个模块,这两个模块都可以单独加载,尽管修复循环依赖的细节对于每种情况都是独一无二的。
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')
})()
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 中的循环依赖
For me, I got similar error when switched between branches - one used newer ("typescriptish") version of @google-cloud/datastore
packages 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.exports
value.对我来说,在分支之间切换时我遇到了类似的错误 - 一个使用了
@google-cloud/datastore
包的较新(“typescriptish”)版本,它返回带有 Datastore 构造函数的对象作为导出对象的属性之一,我切换到另一个分支任务,那里使用了较旧的数据存储版本,它将数据存储构造函数“直接”导出为module.exports
值。 I got the error because node_modules still had newer modules used by branch I switched from.我收到错误是因为 node_modules 仍然有我切换的分支使用的较新模块。
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 */
}
只需在需要文件的地方包装 Arrow 函数
I've faced to something like this too.我也遇到过这样的事情。 in your routes file , export the function as an object like this :
在您的路由文件中,将函数导出为这样的对象:
module.exports = {
hbd: handlebar
}
and in your app file , you can have access to the function by .hbd and there is no ptoblem ....!并且在您的应用程序文件中,您可以通过 .hbd 访问该功能,并且没有 ptoblem ....!
In my case i fix when i put the S in the module.exportS,在我的情况下,当我将 S 放入 module.exportS 时,我会修复,
BEFORE:前:
module.export = () => {}
AFTER:后:
module.exports = () => {}
I don't know how but in may case it got fixed when I changed我不知道如何,但在可能的情况下,当我改变时它会得到修复
require('./routes')(app)
to到
require('./routes')
In my case i fix it when i put the ( ; ) at the end.在我的情况下,当我把 ( ; ) 放在最后时,我会修复它。
BEFORE:前:
const got = require('got')
AFTER:后:
const got = require('got');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.