I am an amateur nodejs developer. I am having deep problems with javascript modules and requiring modules from inside other modules. Here is the scenario I need help with.
Lets say I have API definitions in one file like so (userApi):
var userFunctions = require('./userFunctions.js')()
module.exports = function(){
module.getUser = function(req.res){
userFunctions.getUser(req.id, function(err,user){
if(err) return res(err)
return res(null,user)
})
}
return module;
}
a helper module like so (userFunctions.js)
var paymentFunctions = require('../payment/paymentFunctions.js')()
module.exports = function(){
module.getUser = function(id,res){
//logic
}
return module;
}
another helper module like so (paymentFunctions.js)
var userFunctions = require('../user/userFunctions.js')()
module.exports = function(){
module.makePayment = function(req,res){
userFunctions.getUser(req.id, function(err){
if(err) return res(err)
return res(null)
})
}
return module;
}
As you can see all my helpers and APIs are inside of modules. I also put parenthesis at the end of the require statements. You might also notice that paymentFunctions requires userFunctions and vice versa. During my time with node I have yet to figure out the various errors that come with this pattern. Today I am seeing the "require is not a function error". Note: It was working and suddenly stopped working when I re indexed my webstorm project. That being said I have have many similar errors in the past with require statements and JS modules. Therefore it is a more systemic issue that I need to resolve in my brain.