How does require('atom') work?


#1

I’ve gone into the deep end of trying to understand how Atom’s package management works so that I may implement something similar in my own electron app. Specifically I was trying to find out how require(‘atom’) gives you the global API for Atom without a package needing to depend on an ‘atom’ npm package.

I’ve answered my own question in this StackOverflow question but I’d like if someone could verify if my understanding of how Atom works is correct. Also, if there’s anything else I should know about how the package loading system works, that would be helpful.


#2

You don’t even need to use require('atom') to access the global Atom API. You can just use it by referencing atom without a require.

While Atom does use the standard Node require function, it customizes the Node module cache extensively:


#3

Between using require and accessing the global, what’s considered the best practice? I thought using globals (like mocha/jasmine) was frowned upon in node. I checked one of the core packages, and it seems like require(‘atom’) is used in the source and the global is used in specs.


#4

They’re used for different things. If you need access to certain classes such as Disposable or CompositeDisposable, then you’ll need to use require('atom'). But if you need access to what most people refer to as “the Atom API”, then you would use the atom global to access that.


#5

I’ve got one last question. I’m looking into a bug where fuzzy-finder throws on require 'atom' in its indexing Task. From what I can tell, a Task is run as a forked child process with only node facilities. Yet I don’t see how require 'atom' works from inside of a Task at all, since module-cache.coffee isn’t used in that forked process. How does it work?


#6

Figured it out, never mind! Atom adds the exports directory to NODE_PATH env variable, so it is loaded via node’s normal require.