Module in NodeJS
Every file in a node application is considered a module. But first, we need to understand the core concept of global scope in client-side JavaScript and NodeJS environment.
In JavaScript, several time we use the different objects and functions which we hadn’t defined but it’s usable in all over the application. These all belong to the global scope and accessible all over the applications. Some of them are below:
console.log()
setTimeOut()
setInterval()
clearInterval()
In client-side JavaScript, the window object presents the global scope. So, if we want to access all objects and functions which are globally scoped either direct writing their names or with a window prefix. If we don’t add the window the JavaScript engine will add it their own. Secondly, all variables in client-side JavaScript defined by the user are always globally scoped and will be part of the window object. If we defined the variable twice in client-side JavaScript it will override the other variable.
While we are building large-scale applications, we always divide our code into several files. If we had defined a function with the name of “calculate” in one file and later on defined the other function with the same name “calculate” in another file, it will override the previous function. That’s a big problem with client-side JavaScript.
That’s why in node we don’t have the window object. We had another object which is called the “global”. All predefined JavaScript functions and variables are accessible through that global object. Either we can prefix them with global or we can directly write it to the code. But here is the one main difference to the NodeJS global object, the variables or functions declared by a user will not be added to the global object. They are only scoped to that particular file and they will not be available outside of that file. In object-oriented programming terms, we can say that they are private and are not available outside of the container or module. If we want to make a variable accessible outside of that container, export it or made it public.
How has code run inside the module?
Before the execution of code, the node takes the all-file contents and places them inside a wrapper function. This wrapper function is immediately invoked function expression and named uniquely. This wrapper function has its scope and the code written inside of this function is not accessible outside of the function. This is the main reason why all variables var, let and const are scoped to a particular module rather than globally scoped.
For example, we had the two modules Module A and Module B. Module A have some variables, and module B has some of the variables. Now the module A variable will be over access module B variable module B variables until they exported. If there are some variables in module A and the same name variables in module B then it will never conflict with them because both are in different functional scope.
Each module in Nodejs has 5 parameters, exports, require, module, __filename, and __dirname. Each of them provides a different functionality in NodeJS and very useful while we are building a system.
Takeaway:
A file in NodeJS is called a module. NodeJS environment converts it into a wrapper function before compile.
Every NodeJS application has one file or one module which is called the main module.