ExpressJS Modules
Module Exports - If we import the same module in two files, will we get the same object reference?β
βIf we import the same module in two files, will we get the same object reference?β
In short, yes. But the longer answer is βIt depends.β Node.js will cache requests for a given module to save time later. But, you canβt rely on this functionality, as sometimes the caching system doesnβt work as expected
RequireJSβ
CommonJS defines a module format. Unfortunately, it was defined without giving browsers equal footing to other JavaScript environments. Because of that, there are CommonJS spec proposals for Transport formats and an asynchronous require.
RequireJS tries to keep with the spirit of CommonJS, with using string names to refer to dependencies, and to avoid modules defining global objects, but still allow coding a module format that works well natively in the browser. RequireJS implements the Asynchronous Module Definition (formerly Transport/C) proposal.
If you have modules that are in the traditional CommonJS module format, then you can easily convert them to work with RequireJS. Not all modules will convert cleanly to the new format. Types of modules that may not convert well
data-main attributeβ
data-main is a way to perform the initial require call of your application.
entry point to set configuration options and then load the first application module. Note: the script tag require.js generates for your data-main module includes the async attribute. This means that you cannot assume that the load and execution of your data-main script will finish prior to other scripts referenced later in the same page.
For example, this arrangement will fail randomly when the require.config path for the 'foo' module has not been set prior to it being require()'d later
<script data-main="scripts/main" src="scripts/require.js"></script>
OR
<script src="scripts/require.js"></script>
<script>require(["scripts/main"])</script>