Home > Net >  Javascript - 'x' is not a function
Javascript - 'x' is not a function

Time:06-23

In my javascript file I am calling function B inside function A. I get the following error

B is not a function.

How can I resolve this error?

exports.createRecord = function A() {
  B();

};

exports.B = () =>{
}

CodePudding user response:

Assigning a property to an object does not put that property's name into scope as a standalone identifier. For similar reasons, the following will fail too:

const obj = {
  fn() {
    console.log('hi');
  }
};
fn();

And module.exports is just an object with the same sort of behavior.

Either do

exports.createRecord = function A() {
  exports.B();
};

or

exports.createRecord = function A() {
  B();
};

const B = () => {
};
exports.B = B;
  • Related