Are node modules run when they are required?
For example: You have a file foo.js that contains some code and some exports.
When I import the file by running the following code
var foo = require(./foo.js);
is all the code inside the file foo.js run and only exported after that?
If the module is native, it calls the NativeModule. require() with the filename and returns the result. Otherwise, it creates a new module for the file and saves it to the cache. Then it loads the file contents before returning its exports object.
It implies the file name of the current module. It refers to the directory name of the current module. Note that every file in Node. js will wrap every file that it executes.
As building blocks of code structure, Node. js modules allow developers to better structure, reuse, and distribute code. A module is a self-contained file or directory of related code, which can be included in your application wherever needed. Modules and the module system are fundamental parts of how Node.
In Node. js, Modules are the blocks of encapsulated code that communicates with an external application on the basis of their related functionality. Modules can be a single file or a collection of multiples files/folders.
Requiring a module in Node isn’t that complicated of a concept. The main object exported by the require module is a function (as used in the above example). When Node invokes that require () function with a local file path as the function’s only argument, Node goes through the following sequence of steps:
If no file is find, it will try to find folder with index.js in it . Else it will go to node_modules/ and try to load module from here . If file is still not found , then an error is thrown . Wrapping: once the module is loaded , the module code is wrapped in a special function which will give access to a couple of objects.
The require module, which appears to be available on the global scope — no need to require ('require'). The module module, which also appears to be available on the global scope — no need to require ('module'). You can think of the require module as the command and the module module as the organizer of all required modules.
The main object exported by require () module is a function. When Node invokes that require () function with a file path as the function’s only argument, Node goes through the following sequence of steps: Let’s look at each step in more detail. When require function receive the module name as its input, It first tries to load core module.
Much like in a browser's <script>
, as soon as you require a module the code is parsed and executed.
However, depending on how the module's code is structured, there may be no function calls.
For example:
// my-module-1.js // This one only defines a function. // Nothing happens until you call it. function doSomething () { // body } module.exports = doSomething; // my-module-2.js // This one will actually call the anonymous // function as soon as you `require` it. (function () { // body })();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With