'TypeError: is not a function' in Node.js

前端 未结 8 1830
礼貌的吻别
礼貌的吻别 2021-02-03 16:48

I\'m getting the error while running the following code in Node.js

var assert = require(\'assert\');
var request = require(\'request\');
var index = require(\'./         


        
相关标签:
8条回答
  • 2021-02-03 17:37

    One silly mistake I did was while exporting was:

    module.exports = [module_name_1, module_name_2, ..., module_name_n]
    

    The right way is:

    module.exports = {module_name_1, module_name_2, ..., module_name_n}
    
    0 讨论(0)
  • 2021-02-03 17:39

    I'm fairly a beginner at Node JS so I managed to get this error by importing a function like so:

    const { functionName } = require('./function')
    

    instead of like so:

    const functionName = require('./function')
    

    Editing my post to add an explanation since I've learned more node since I wrote it. If a module exports an object containing multiple functions functions like so:

    module.exports = { functionName, otherFunction }
    

    Then the function has to be deconstructed out of the object during the import, as in the first code snippet. If the module exports a single function or a default function, like so:

     module.exports = functionName
    

    Then tt must be imported directly, as in the second code snippet.

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