Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Node run all the code inside required modules?

Tags:

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?

like image 461
Victorino Machava Avatar asked Nov 07 '16 11:11

Victorino Machava


People also ask

What happens when we require a module in NodeJS?

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.

Does the node wrap all the modules?

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.

How do modules work in node?

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.

What do node modules contain?

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.

How does requiring a module in node work?

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:

What happens if no file is found in node_modules?

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.

What is the difference between the require and the module Module?

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.

What happens when node invokes require() function?

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.


1 Answers

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 })(); 
like image 92
tmslnz Avatar answered Oct 21 '22 08:10

tmslnz