7

I have an ErrorHandler class in my node code that handles all occuring errors.

Every Error has its own class. All classes are placed in seperate files so the folder structure is:

  • ErrorHandler.js
    • Error110.js
    • Error131.js
    • ...

ErrorHandler requires each of these files (along with some others)

var serverErrorCodes        = require('../../config/ErrorCodesConfig').server,
clientErrorCodes            = require('../../config/ErrorCodesConfig').client,
Error110                    = require('./errors/Error110').Error110,
Error131                    = require('./errors/Error131').Error131,
Error132                    = require('./errors/Error132').Error132,
Error133                    = require('./errors/Error133').Error133,
Error150                    = require('./errors/Error150').Error150;

When checking in node-debugger during runtime, the require result ist:

Closure:
  Error110: function Error110(params)
  Error131: function Error131(params)
  Error132: undefined
  Error133: undefined
  Error150: function Error150(messageObject, callback)

Error132 and Error133 are not available after the require, while the others are perfectly.

I've read that this happens if you have a require cycle and thus a required module (A) is requiring a module (B) which again requires module A. Node then ends this cycle by returning undefined after the first require. This is not the case.

So my last guess is that, at the time of execution, due to the asynchronous nature of node, the files are "not yet" required and thus only "some" files get included. But require is a synchronous function. I am not certain if this scenario is possible and how I would check for it.

I am clueless here, help is much appreciated.

Best regards, Worp

Edit 1:
As Travis Webb pointed out: This is wrong!
It also happens if your require a module that is already included somewhere earlier in the code. I thought about this but I am not requiring a module. And over all this doesn't seem to be true, since, for example, the util module needs to be included in every file that it is used, instead of just once.

5
  • Are you sure that the files inside are correct? What does console.log(require('./errors/Error132')) show? What are the contents of the Error132 and Error133 files? Commented Jul 31, 2014 at 16:11
  • Thanks for the quick response! I am pretty certaint he contents are correct. At least I have tried to copy/paste the contents of a working file into a failing file (changing the class names via find/replace, paying attention to what was getting replaced) and tried to require it with the same outcome. Since I am currently travelling and don't have access to the code, I will post the failing files asap once I get back.
    – Worp
    Commented Aug 1, 2014 at 16:18
  • So! It's been a while. Sorry about that. It turns out, you were right. A colleague of mine committed a patch that changed one number in each file, turning the export of Error133 to "Error132" and in Error133 to "Error132". I am very sorry for this stupid mistake, I just didnt spot it... Can you make your comment an answer, so I can mark it correct?
    – Worp
    Commented Aug 23, 2014 at 11:07
  • So do you want to mark my answer as correct? Commented Nov 14, 2019 at 17:51
  • 1
    Hey Travis =) I feel like your post is ultimately the more important one for the overall info contained in it. It was not the answer to this question though, as Jessie's answer actually got me to figuring it out. I feel like your answer below will continue to receive the attention it deserves and upvotes for it =)
    – Worp
    Commented Nov 15, 2019 at 7:26

2 Answers 2

28

A simpler explanation is that module.exports is resolving to undefined inside the module that you are requiring. Post the code of the failing modules.

It also happens if your require a module that is already included somewhere earlier in the code.

This statement is incorrect.

due to the asynchronous nature of node, the files are "not yet" required

require is synchronous.

It is correct that if module A requires B, which in turn requires A, then A will be undefined in module B. But I'm not sure this is the problem.

7
  • 1
    Hey Travis, as you can read from my comment above, I was stupid and overlooked the typing error my colleague had brought in. Nevertheless your point is very important, besides of me already editing it in my original post. Since I don't have enough reputation yet to give points, is there anyhting I can do to mark your answer as important?
    – Worp
    Commented Aug 23, 2014 at 11:12
  • 1
    Continue using Stackoverflow :) I'm patient. Commented Aug 23, 2014 at 18:24
  • 1
    Still, thx a lot! I will remember your comment. Only 5 reputation to go until I can upvote it :D
    – Worp
    Commented Aug 23, 2014 at 21:04
  • 2
    How about now? :) Commented Nov 14, 2019 at 17:51
  • 1
    This man Travis really came back 5 years later to get that upvote. That's due diligence right there.
    – Musilix
    Commented Mar 24, 2023 at 17:16
-1

Are you sure that the files inside are correct? What does console.log(require('./errors/Error132')) show? What are the contents of the Error132 and Error133 files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.