Nodejs modules - getting them to work

Nodejs modules - getting them to work


I have code from a non-FCC tutorial, a simple module and a very small program that uses the module, that supposedly works.

(In order to figure out what is needed to solve one of the Start a Nodejs Server FCC challenges, this is a tutorial I turned to to find out more about how nodejs ticks).

However the tutorial is not explicit about where to put the two files in node.js and get them to talk to each other.

For example, I put both the module, called “mulitply.js” and the code that uses it, called “test.js” in the same directory (side by side in the same file). I ran node test.js and got the “Error: Cannot find module” error.

I then created a folder in that same directoy called “modules” and put “mulitply.js” in that folder, then ran test.js, and go the same error (“Error: Cannot find module”)

What am I missing? Why is it that I am getting this error? I am fairly certain the code is correct (because I did not write it!)


// require any modules

module.exports = {  
    multiply: function (val1, val2, callback){
        var returnedValue = val1 * val2;
        callback(null, nonPublic() + returnedValue);

function nonPublic(){  
    return 'Result: ';


var mod = require('./module');


mod.multiply(5, 10, function(err, result){  


I was using this tutorial:


You use var mod = require('./module'); but your file is called multiply.js. So you need to change either the file name to module.js or change the code to: var mod = require('./multiply');


Nice! Actually I changed it to var mod = require(’./multiply.js’); and that worked.

Then I moved multiply.js out of the “modules” folder and back into the same directory.

I ran it and got the error for ‘TypeError: mod.nonPublic is not a function’ (as was expected per the tutorial), then commented out console.log(mod.nonPublic()); from test.js per the tutorial, re-ran it, and it works as expected.

Thanks for your help!