Nodejs: "required" module in Nodejs does not work with a specific file name

For a long time, asking a question for the first time.

I have a file (let him call it file.js) in which I try to require another file called user.service.js at the top of the file:

var userService = require('./user.service'); 

I am sure that the path is correct and that the user.service.js file exports the populated object:

In user.service.js:

 module.exports = { signIn: signIn, signUp: signUp, updateProfile: updateProfile }; 

However, userService is always an empty {} object. It is strange if I recreate a file with a different name in the same directory (for example, user.service2.js), the require statement works correctly. Or if I rename file.js to something else, for example. file2.js, the call is working. Also, if I need a file inside a function in user.service.js, the instruction also works. However, I would prefer the require statement to be at the top of the file and be available to all functions inside it.

Thanks in advance for your help.

Edit:

Here is a sample code:

 var userService = require('./user.service'); var testFunc = function () { console.log(userService); // this logs: {} var userServiceInternal = require('./user.service'); console.log(userServiceInternal); // This logs: // { // signIn: [Function], // signUp: [Function], // updateProfile: [Function] // } }; 
+5
source share
1 answer

I realized this ... I had a circular addiction. Thanks for the comments!

+2
source

All Articles