I had two javascript files in Node Project. One was router which had routes and corresponding functions in it. Another was a utility class where commonly used functions are written as prototype. say utilservice.getSomething();
The first file calls utilservice.getSomething() in second file.
I imported the first file in second file (which is unnecessary and unused) .Then i called an API in first file.
I got error 500 stating utilservice.getSomething() is not a function.
I spent so much hours thinking something went wrong with use of Promise and tried with async and await and landed in same error.
Very last, i removed the import and found API call happening well.
I was with an impression that require is just for import of methods in another script. But there's something beyond it. There are resources online which would tell purpose of request. But i like to understand this behavior.
Sample:
File1.js
const utilService = require('../utils/utilService');
router.get('/something',function(req,res){
utilService.getSomething().then((data)=>{
//do something
})
});
File2.js
const file = require('../file1');
function util(){}
util.prototype.getSomething = function(){
return "hello"
}
module.exports = new util();
I hit that /something API. I got utilservice.getSomething is not a function.