close

[Solved] NodeJs : TypeError: require(…) is not a function

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error NodeJs : TypeError: require(…) is not a function in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How NodeJs : TypeError: require(…) is not a function Error Occurs?

Today I get the following error NodeJs : TypeError: require(…) is not a function in javascript.

How To Solve NodeJs : TypeError: require(…) is not a function Error ?

  1. How To Solve NodeJs : TypeError: require(…) is not a function Error ?

    To Solve NodeJs : TypeError: require(…) is not a function Error Inside your ../routes file you need to explicitly export the important function:

  2. NodeJs : TypeError: require(…) is not a function

    To Solve NodeJs : TypeError: require(…) is not a function Error Inside your ../routes file you need to explicitly export the important function:

Solution 1


Your ../routes source file is not exporting a function.

Try replacing this line:

app.use('/', require('../routes')(express));

with:

console.log( require('../routes') );

I bet it prints an object ({ ... }) rather than a function.


Inside your ../routes file you need to explicitly export the important function:

module.exports = importantFunction;

function importantFunction(string) {
  console.log(string);
}

Now you scripts which import it can call it.

require('../routes')("hi") // should log "hi"

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment