0

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
4
  • You are calling sum before it's created. If you want to keep the sum creation at the same place, you need to make it a function declaration, otherwise you can just move it up. Commented Apr 14, 2020 at 18:13
  • 1
    it will be helpers.sum(10,20); not only sum Commented Apr 14, 2020 at 18:14
  • ...and that's why we ask for a minimal reproducible example. I got mislead by the code since it was not representative. Commented Apr 14, 2020 at 18:16
  • I believe you have to refer to the sum method through the object assigned by the require('./helpers') statement ie const total = helpers.sum(10,20); Commented Apr 14, 2020 at 18:18

4 Answers 4

1

Try this way:

   //app.js

const helpers= require('./helpers');


const total= helpers.sum(10,20);
console.log("total:",total);

//helpers.js

const sum = (a,b)=> {
    return a+b;
}

module.exports = sum;
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you are exporting your function sum in helpers.js within an object:

// helpers.js

// ...

module.exports = {
    sum
};

If you want to access it from app.js you can either use helpers.sum(...) to access it:

// app.js

const helpers = require('./helpers');


const total = helpers.sum(10, 20);
console.log("total:", total);

... or you can use object deconstruction in the require line:

// app.js

const { sum } = require('./helpers');

// ...

Update

Sure, the answer you posted does work, but from the name helpers.js I assume that you might want to put multiple helper functions into that file. Then, you must use an object export - as mentioned above - in order to export all helper functions.

Comments

0

You can try to edit the way to export the sum function to:

module.exports = sum;

That's because when you define:

module.exports = {...}

it means you're exporting an object, not a function.

Usage:

const sum= require('./helpers');

const total= sum(10,20);
console.log("total:",total);

Comments

0

Finally, I found a my answer.

I just needed to replace the helpers keyword with sum keyword in the first line of app.js and I also removed the curly braces around sum in helpers.js as per other's advice.

The correct code is below

//app.js

const sum= require('./helpers');
const total= sum(10,20);

console.log("total:",total);

and

//helpers.js

const sum = (a,b)=>{
return a+b;
}
module.exports=sum;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.