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

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

提交回复
热议问题