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











up vote
24
down vote

favorite
2












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



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


The index.js file contain the following code:



function AddNumbers(a,b){
return a+b;
}


What am I doing wrong?










share|improve this question




















  • 3




    @ckruczek: It's an ESL/cultural thing, it's not meant rudely, despite how it comes across in written English. I've replaced it with something more appropriate (er, I hope).
    – T.J. Crowder
    Nov 23 '15 at 6:59

















up vote
24
down vote

favorite
2












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



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


The index.js file contain the following code:



function AddNumbers(a,b){
return a+b;
}


What am I doing wrong?










share|improve this question




















  • 3




    @ckruczek: It's an ESL/cultural thing, it's not meant rudely, despite how it comes across in written English. I've replaced it with something more appropriate (er, I hope).
    – T.J. Crowder
    Nov 23 '15 at 6:59















up vote
24
down vote

favorite
2









up vote
24
down vote

favorite
2






2





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



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


The index.js file contain the following code:



function AddNumbers(a,b){
return a+b;
}


What am I doing wrong?










share|improve this question















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



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


The index.js file contain the following code:



function AddNumbers(a,b){
return a+b;
}


What am I doing wrong?







javascript node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 20:28









Hongarc

2,1401723




2,1401723










asked Nov 23 '15 at 6:42









Karthick Gk

125118




125118








  • 3




    @ckruczek: It's an ESL/cultural thing, it's not meant rudely, despite how it comes across in written English. I've replaced it with something more appropriate (er, I hope).
    – T.J. Crowder
    Nov 23 '15 at 6:59
















  • 3




    @ckruczek: It's an ESL/cultural thing, it's not meant rudely, despite how it comes across in written English. I've replaced it with something more appropriate (er, I hope).
    – T.J. Crowder
    Nov 23 '15 at 6:59










3




3




@ckruczek: It's an ESL/cultural thing, it's not meant rudely, despite how it comes across in written English. I've replaced it with something more appropriate (er, I hope).
– T.J. Crowder
Nov 23 '15 at 6:59






@ckruczek: It's an ESL/cultural thing, it's not meant rudely, despite how it comes across in written English. I've replaced it with something more appropriate (er, I hope).
– T.J. Crowder
Nov 23 '15 at 6:59














3 Answers
3






active

oldest

votes

















up vote
49
down vote













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






share|improve this answer





















  • You saved me today :)
    – arslan
    Dec 21 '16 at 11:46










  • You saved me hours of banging my head to the wall. Thank you!
    – mephisto123
    Jan 13 at 12:42










  • indeed saver! not much said...directly to the point :)
    – oetoni
    Aug 13 at 16:50


















up vote
28
down vote













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





share|improve this answer























  • I tried the above line still i'm getting the same error.
    – Karthick Gk
    Nov 23 '15 at 12:11






  • 1




    @KarthickGk: Then something else is wrong, the above works.
    – T.J. Crowder
    Nov 23 '15 at 12:32










  • Thanks a lot now its working fine.
    – Karthick Gk
    Nov 23 '15 at 13:28




















up vote
2
down vote













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
};





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33865068%2ftypeerror-is-not-a-function-in-node-js%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    49
    down vote













    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






    share|improve this answer





















    • You saved me today :)
      – arslan
      Dec 21 '16 at 11:46










    • You saved me hours of banging my head to the wall. Thank you!
      – mephisto123
      Jan 13 at 12:42










    • indeed saver! not much said...directly to the point :)
      – oetoni
      Aug 13 at 16:50















    up vote
    49
    down vote













    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






    share|improve this answer





















    • You saved me today :)
      – arslan
      Dec 21 '16 at 11:46










    • You saved me hours of banging my head to the wall. Thank you!
      – mephisto123
      Jan 13 at 12:42










    • indeed saver! not much said...directly to the point :)
      – oetoni
      Aug 13 at 16:50













    up vote
    49
    down vote










    up vote
    49
    down vote









    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






    share|improve this answer












    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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 17 '16 at 16:43









    shimi_tap

    4,83941621




    4,83941621












    • You saved me today :)
      – arslan
      Dec 21 '16 at 11:46










    • You saved me hours of banging my head to the wall. Thank you!
      – mephisto123
      Jan 13 at 12:42










    • indeed saver! not much said...directly to the point :)
      – oetoni
      Aug 13 at 16:50


















    • You saved me today :)
      – arslan
      Dec 21 '16 at 11:46










    • You saved me hours of banging my head to the wall. Thank you!
      – mephisto123
      Jan 13 at 12:42










    • indeed saver! not much said...directly to the point :)
      – oetoni
      Aug 13 at 16:50
















    You saved me today :)
    – arslan
    Dec 21 '16 at 11:46




    You saved me today :)
    – arslan
    Dec 21 '16 at 11:46












    You saved me hours of banging my head to the wall. Thank you!
    – mephisto123
    Jan 13 at 12:42




    You saved me hours of banging my head to the wall. Thank you!
    – mephisto123
    Jan 13 at 12:42












    indeed saver! not much said...directly to the point :)
    – oetoni
    Aug 13 at 16:50




    indeed saver! not much said...directly to the point :)
    – oetoni
    Aug 13 at 16:50












    up vote
    28
    down vote













    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





    share|improve this answer























    • I tried the above line still i'm getting the same error.
      – Karthick Gk
      Nov 23 '15 at 12:11






    • 1




      @KarthickGk: Then something else is wrong, the above works.
      – T.J. Crowder
      Nov 23 '15 at 12:32










    • Thanks a lot now its working fine.
      – Karthick Gk
      Nov 23 '15 at 13:28

















    up vote
    28
    down vote













    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





    share|improve this answer























    • I tried the above line still i'm getting the same error.
      – Karthick Gk
      Nov 23 '15 at 12:11






    • 1




      @KarthickGk: Then something else is wrong, the above works.
      – T.J. Crowder
      Nov 23 '15 at 12:32










    • Thanks a lot now its working fine.
      – Karthick Gk
      Nov 23 '15 at 13:28















    up vote
    28
    down vote










    up vote
    28
    down vote









    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





    share|improve this answer














    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '15 at 12:38

























    answered Nov 23 '15 at 6:46









    T.J. Crowder

    669k11611841279




    669k11611841279












    • I tried the above line still i'm getting the same error.
      – Karthick Gk
      Nov 23 '15 at 12:11






    • 1




      @KarthickGk: Then something else is wrong, the above works.
      – T.J. Crowder
      Nov 23 '15 at 12:32










    • Thanks a lot now its working fine.
      – Karthick Gk
      Nov 23 '15 at 13:28




















    • I tried the above line still i'm getting the same error.
      – Karthick Gk
      Nov 23 '15 at 12:11






    • 1




      @KarthickGk: Then something else is wrong, the above works.
      – T.J. Crowder
      Nov 23 '15 at 12:32










    • Thanks a lot now its working fine.
      – Karthick Gk
      Nov 23 '15 at 13:28


















    I tried the above line still i'm getting the same error.
    – Karthick Gk
    Nov 23 '15 at 12:11




    I tried the above line still i'm getting the same error.
    – Karthick Gk
    Nov 23 '15 at 12:11




    1




    1




    @KarthickGk: Then something else is wrong, the above works.
    – T.J. Crowder
    Nov 23 '15 at 12:32




    @KarthickGk: Then something else is wrong, the above works.
    – T.J. Crowder
    Nov 23 '15 at 12:32












    Thanks a lot now its working fine.
    – Karthick Gk
    Nov 23 '15 at 13:28






    Thanks a lot now its working fine.
    – Karthick Gk
    Nov 23 '15 at 13:28












    up vote
    2
    down vote













    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
    };





    share|improve this answer

























      up vote
      2
      down vote













      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
      };





      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        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
        };





        share|improve this answer












        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
        };






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 6:38









        Aravinda Meewalaarachchi

        509311




        509311






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33865068%2ftypeerror-is-not-a-function-in-node-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python