44

I have just started working with nodejs. I wonder if there is a way to "require" a file only once in an app. I am using a class framework for getting classic OOPS in my JS project. Each "class" is contained in its own JS file. I want to "require" the class framework in each file so that they can function independently but want the framework's init code to be executed only once.

I can use a flag to implement this myself but a built-in way would be nice. Search for "require once" leads me to all PHP related questions.

atlantis
  • 817
  • 1
  • 10
  • 16

2 Answers2

117

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

timoxley
  • 5,156
  • 3
  • 36
  • 40
  • Can you explain or give example when you say `run initialisation code multiple times, simply throw that code into an exported method.` –  Oct 22 '15 at 08:58
  • 1
    @sudo rather than leaving the code in the body of the module, put it into a function that you export. Modules requiring your module can then execute this function in order to rerun the initialisation code. e.g. ```js var db = connect(); module.exports = db ``` vs ```js module.exports = connect ``` Another way to think of it is: Node modules are always singletons. Your singleton can expose a constructor/factory to create instances. – timoxley Oct 23 '15 at 07:16
  • Thanks for the response, and I think I understand what you are saying. Definitely a good fix if someone has to refractor upon learning that their modules are cached and being re-used, rather than re-initialized. Personally though I think I am going to just follow the `new` and `prototype` style to ensure objects are actually re-initialized. What are your thoughts on that pattern, if you are familiar? –  Oct 23 '15 at 22:37
  • Good pattern. I suggest using ES6 class keyword to make things less verbose though. – timoxley Oct 24 '15 at 15:01
  • If someone wants to reinitialize the module function, s/he should simply recall the module.exports's function after require – sundowatch May 31 '20 at 17:14
  • I use const path = require('path') in 2 locations/lib ts files. VScode complaints, that `path was also declared here - cannot redeclare block-scoped var path` – Timo Apr 15 '22 at 14:24
  • @Timo That sounds like an unrelated issue with using `const ` with the same variable name. Delete the second one and it should just work. – timoxley May 19 '22 at 13:49
8

If you really want the top level code in your module (code that is not contained within methods or functions in your module) to execute more than once you can delete it's module object that is cached on the require.cache object like this:

delete require.cache[require.resolve('./mymodule.js')];

Do this before you require the module for the second time.

Most of the time though you probably only want the module's top level code to run once and any other time you require the module you only want to access what that module exports.

var myMod = require("./mymodule.js"); //the first time you require the
                                      //mymodule.js module the top level code gets
                                      //run and you get the module value returned.


var myMod = require("./mymodule.js"); //the second time you require the mymodule.js
                                      //module you will only get the module value
                                      //returned. Obviously the second time you
                                      //require the module it will be in another
                                      //file than the first one you did it in.
Larry Lawless
  • 613
  • 6
  • 11