I created two files, app.js and helpers.js and when I tried to call a function from app.js, I got an error, function is not defined.
The two files are located in the same folder. I think there is a problem the module.exports keyword,
can anyone help me here? Here is the code of the two separate files:
//app.js
const helpers= require('./helpers');
const total= sum(10,20);
console.log("total:",total);
//helpers.js
const sum = (a,b)=>{
return a+b;
}
module.exports={
sum
};
And the error i am getting is:
const total= sum(10,20);
^
ReferenceError: sum is not defined
at Object.<anonymous> (E:\testing\app.js:5:14)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
sumbefore it's created. If you want to keep thesumcreation at the same place, you need to make it a function declaration, otherwise you can just move it up.helpers.sum(10,20);not onlysum