Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 236
atlantis Avatar asked Jan 22 '12 00:01

atlantis


People also ask

Can you require a js file?

1) require()require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.

How do I use require in node JS?

You can think of the require module as the command and the module module as the organizer of all required modules. Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file'); The main object exported by the require module is a function (as used in the above example).

What can I use instead of require JS?

Use Import Instead of Require in Node App.

What is require FS in node JS?

js file system module allows you to work with the file system on your computer. To include the File System module, use the require() method: var fs = require('fs'); Common use for the File System module: Read files.


2 Answers

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

like image 127
timoxley Avatar answered Oct 05 '22 21:10

timoxley


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.
like image 31
Larry Lawless Avatar answered Oct 05 '22 20:10

Larry Lawless