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

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

    With NodeJS modules, to make something public, you have to export it. Add this to the end of index.js:

    module.exports.AddNumbers = AddNumbers;
    

    Here it is running on my machine:

    $ cat index.js 
    function AddNumbers(a,b){
        return a+b;
    }
    
    module.exports.AddNumbers = AddNumbers;
    
    $ cat example.js 
    var index = require('./index');
    var v2 = index.AddNumbers(5,6);
    console.log(v2);
    
    $ node example.js
    11
    
    0 讨论(0)
  • 2021-02-03 17:22

    I ran into the same problem while trying to follow a Nodejs tutorial by w3schools. I copied the following code from them:

    exports.myDateTime = function () {
    return Date();
    };
    

    That, however, wouldn't work for me. What resolved the problem for me was adding module. before the exports keyword like this:

    module.exports.myDateTime = function () {
    return Date();
    };
    
    0 讨论(0)
  • 2021-02-03 17:23

    If you need to expose a specific component, function or a variable to public. You have to exports those components using JavaScript modules.

    let add = (a,b)=>{
        return ( a+b);
    }
    module.exports.add=add;
    

    or if you want to expose multiple functions, you can do as follows.

    let add = (a,b)=>{
        return (a+b);
    }
    let subtract = (a, b)=>{
        return (a-b);
    }
    module.exports={
       add : add,
       subtract : subtract
    };
    
    0 讨论(0)
  • 2021-02-03 17:25

    Your "AddNumbers" function in the "index.js" file should be as follows,

    function AddNumbers(a,b){
        var addition = function(a, b){
          return (a + b) ;
        };
    
        module.exports = {
           additionResult: addition
        };
    }
    

    And you need to call it in your "Node.js" file as follows

    var assert = require('assert');
    var request = require('request');
    var index = require('./index');
    it('verify javascript function', function(done) {
        var v2 = index.additionResult(5, 6);
        assert.equal(11, v2);
        done();
    });
    

    This should work. Please note that you call the function by whatever the token name you exported the return value by (I use a different name here just for clarity). Almost everybody uses the same name as the function name so there are no confusion. Also in ES6, if you use the same name you can export as just,

    module.exports = {
           addition
    };
    

    instead of,

    module.exports = {
           addition: addition
    };
    

    since you use the same name. It is an ES6 feature.

    0 讨论(0)
  • 2021-02-03 17:31

    This happened to me many times because of circular dependency, check if you have 2 classes that are requiring each other, remove one of them from requiring the other and the issue should be solved

    0 讨论(0)
  • 2021-02-03 17:37

    This is happening because two files are referencing each other i.e You are calling function (s) from file A in file B and vice versa which is called Circular Dependency.

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