140

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

Here are my code and error:

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

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:

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

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
taigi100
  • 2,609
  • 4
  • 23
  • 42
  • Can you `console.log(require)` before the line that fails? You should not need requireJS on the server side, node.js has a module system built in (CommonJS, also uses `require` function). Something seems to be changing the global `require` variable. – kraf Oct 08 '15 at 06:16

8 Answers8

191

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

Error:

const fs = require('fs')

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

Good:

const fs = require('fs');

(() => {
  console.log('wow')
})()
mCY
  • 2,625
  • 4
  • 14
  • 21
  • 21
    This was my problem as well! The problem really is that the runtime ignores whitespace in this case, and so it seems you're passing your function body `() => {}` as an argument to whatever is returned by `require( ... )`. Terrible! – Will Brickner Apr 07 '19 at 06:29
  • 1
    yeah, this is a good argument in favor of semicolons in the 'semicolons in JS' debate. Fortunately, the absence of semicolons seems to be problem only in the context of IIFEs, which are falling out of fashion – JP Lew Jul 15 '19 at 18:13
  • 3
    Oh my god!! I came from `puppeteer` and almost requested an issue there until I found one here. Phew... never thought that `;` would trouble me so much. – Irfandy Jip Jan 12 '20 at 05:42
  • 7
    Great solution! Note that you actually should put the semicolon before every self-invoking function. I adapted this construction: `;(() => {})()` – Dmitry Feb 16 '20 at 16:47
  • 1
    What are the options if the code is within a library? I have a node_modules library (node-mailjet) which does not use semicolons at all. I can build with Webpack ok locally but when I build on Docker then it get this error :( – Javier Guzmán Oct 02 '20 at 19:38
  • Oh my god I can't believe this! I lost HOOOOURS trying to fix this issue and it was just a `;`! – Victor May 17 '21 at 05:33
  • Great man. it worked require("./generateBearerToken");(() => {})() – Hrishikesh Baidya Jun 12 '21 at 06:13
133

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

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
}

You are exporting a function that can then be called like 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 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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Or: `module.exports.functionName = function functionName(app, passport) { ... }` – Magne Oct 15 '20 at 13:37
  • @Magne - But the OP is trying to do `require('./app/routes')(app, passport);` which will not work the way you show the export. – jfriend00 Oct 15 '20 at 20:06
  • True, my bad. My use case was slightly different. I wanted to export a named function. – Magne Oct 19 '20 at 15:52
18

For me, this was an issue with cyclic dependencies.

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

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

How to deal with cyclic dependencies in Node.js

Community
  • 1
  • 1
Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46
3

Remember to export your routes.js.

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

exports = function(app, passport) {

/* write here your code */ 

}
alex.pulver
  • 2,107
  • 2
  • 31
  • 31
2

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. I got the error because node_modules still had newer modules used by branch I switched from.

Kote Isaev
  • 273
  • 4
  • 13
0

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

-1

In my case i fix when i put the S in the module.exportS,

BEFORE:

module.export = () => {}

AFTER:

module.exports = () => {}
Breno AllenCS
  • 573
  • 3
  • 8
-1

I don't know how but in may case it got fixed when I changed

require('./routes')(app)

to

require('./routes')
Samyak Jain
  • 90
  • 12