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

前端 未结 8 1826
礼貌的吻别
礼貌的吻别 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: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.

提交回复
热议问题