Module initializing more than once


#1

I have a module that is “required” once during Atom load time and then again later when initialization is done and my package is running. The module’s initialization code is running both times it is required. This does not normally happen in Node. This causes me a lot of problems.

My guess is that the module cache is somehow reset or a different one is used at the end of Atom initialization. This needs to be fixed.

I will have to figure out how to make the init idempotent but it isn’t going to be easy if even possible.


#2

In case anyone cares …

The cause of this was more mundane that Atom requiring things twice, once during load and once after. That was a coincidence.

The problem was in my usage of require in Node. I had a cyclic dependency of two modules requiring each other. This is actually something I’ve done in server apps before with no problem.

In this case one module’s require was returning the module object before it had finished initializing. It looked to the other module like an empty object. After researching node’s module docs, I found this is to be expected. I guess I had been lucky before.

I solved this by moving all requires to the activation method or equivalent in each module. This got rid of the cyclic requires.

BTW, this is the exact thing that @Zren says to do to speed up Atom loading in general. See How To Speed Up Your Packages.

So kiddies, listen up. Always put your requires in places that run as late in the execution as you can. At least in the activation phase.