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

前端 未结 7 879
旧巷少年郎
旧巷少年郎 2020-11-27 15:27

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

相关标签:
7条回答
  • 2020-11-27 15:56

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

    0 讨论(0)
  • 2020-11-27 15:58

    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')
    })()
    
    0 讨论(0)
  • 2020-11-27 16:06

    Just wrap in Arrow function where you requiring the files

    0 讨论(0)
  • 2020-11-27 16:11

    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 */ 
    
    }
    
    0 讨论(0)
  • 2020-11-27 16:12

    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

    0 讨论(0)
  • 2020-11-27 16:12

    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.

    0 讨论(0)
提交回复
热议问题