Is there a way to “require” a JS file only once in nodejs?

require is always “require once”. After you call require the first time, require uses a cache and will always return the same object.

Any executable code floating around in the module will only be run once.

On the other hand, if you do want it to run initialisation code multiple times, simply throw that code into an exported method.

edit: Read the ‘Caching’ section of http://nodejs.org/docs/latest/api/modules.html#modules

Leave a Comment

tech