But before that let’s discuss the parameters in every node.js module.
Parameters in Every Node.JS Module:
Each Node module has access to the five parameters namely: require, exports, module, __filename, and __dirname.
Despite being universal to the code in a module, some variables are local to the module (because of the function wrapper). These characteristics offer insightful data on a module.
Let’s now get into detail about each of the parameters.
The require module appears to be present on the global scope there is no need to require(‘require’).
The require module may be compared to the directive, and the module can be compared to the organizer of all required modules.
It’s not a really difficult idea to require a module in Node.
const config = require('/path/to/file');
A function is the principal object exported by the required module.
The following steps are performed by Node when it calls the require() function with a local file location as the function’s sole argument:
- Finding the file’s absolute path is the resolution.
- During loading, the type of the file’s content is determined.
- Giving the file its private scope by wrapping. This is the reason why each file we require has a local copy of the require and module objects.
- The loaded code is ultimately subjected to evaluation by the virtual machine (VM).
- Caching: This saves us from having to repeat all the processes each time we need to access the file.
Every JavaScript file in a Node.js application by default contains a special object called module.exports.
The exports object will be presented as a module, whereas the module is a variable that denotes the current module.
Therefore, everything you specify for module.exports will be made available as a module.
What are Modules in NodeJS?
- A Module in Node.js is a grouping within one or more Scripts that can be utilized frequently throughout much of the Node.js application and may be simple or complex in nature.
- Node.js modules may clash with each other or taint the broad reach because each of them has its own environment. A separate.js file for each package may also be placed in a distinct subfolder.
- Think of modules as being similar to JavaScript libraries. A group of features you desire your program to have.
- To adequately structure and organize your programming, you construct modules.
- They can be used to separate big programs into tiny, easier-to-manage, and more autonomous units of software that each perform one or a few related functions.
- In layman’s terms we can see it as: We would arrange our shirts in one drawer and our trousers in another in our wardrobe, even though we mix both to form an outfit for the day.Similarly, we can distinct components of our codebase in different modules and then merge them into a cohesive application.
We hope you got an idea of Modules in NodeJS, it’s now time we move on to the types of modules offered by NodeJS.
Types of Modules in NodeJS
The modules are classified into three types in NodeJS:
- Core Modules
- Local Modules
- Third-party Modules
Let’s now discuss each one of them in order:
Module |
Description |
Timers |
The timer features are all global and pertain to time. You don’t have to use the require() method to load it. |
HTTP |
Both the HTTP server and HTTP client are made using it. You may get to it by using require(“http”). |
VM |
It offers access to the V8 virtual machine for JavaScript code compilation and execution. You may get to it by using require(“vm”). |
Stream |
Data is streamed between two entities using it. You may get to it by using require(“stream”). |
DNS |
It is used to carry out actions on domain names, such as lookup and resolve. You may get to it by using require(“dns”). |
Get 100% Hike!
Master Most in Demand Skills Now!
The modules you generate privately for your Node.js application are known as local modules. In distinct documents and folders, these modules contain
the various application functionalities.
The Node.js users can utilize it if you bundle it and publish it via NPM.
The require() function must be used to load local modules in a similar manner as essential modules in order to utilize them in your program.
But you have to mention the location of the module’s JavaScript file.
Node third-party modules are packages or modules created and maintained by outside entities. There are countless third-party node modules and bundles that are publicly accessible on the NPM Registry.
You can add functionality to your projects by installing third-party node components and bundles.
Gulp, React, Socket.IO, Mongoose, Mocha, Async, Express, Underscore, Lodash, PM2, Bower, Q, Debug, and more examples of top third-party modules. Components from third parties could be installed either locally or worldwide.
The time has now come that we start learning about creating Modules in NodeJS.
Let’s Create Your First Module
Ultimately, a module just outputs an object. Your firstmodule.js is an example of a basic module.
module.exports.name = function() {
return "Monika";
}
This produces an object with only the name property. With code like this, you can load and obtain the value of name:
Const firstModule = require('./firstmodule');
console.log('Results: ' + firstModule.name());
Let’s highlight a few items from this snippet:
First, take note of the fact that my require command began with “./,” indicating a reasonably pathed module and instructing require() to search in the same directory as the currently running module.
Second, you’ll see that we omitted the.js suffix from the module’s end.
A typical practice in Node is to omit the.js suffix.
However, it does leave some room for doubt as to what will actually be loaded. It may allude to:
myfirstmodule as a file
myfirstmodule.js as a file
myfirstmodule.json as a file
The filename of the code being run is represented by the __filename. The determined absolute path to this code file is as follows:
The path for the main program may differ from the one used on the command prompt.
The location of a module’s folder is contained in the value of such a module.
// Let's try printing __filename's value.
console.log( __filename );
It is a private parameter that gives the active module’s directory identity of the active module’s directory. The current JavaScript file’s folder path is returned.
It gives back the name of the directory where the source code file is located.
It is particular to every module.
It relies on the file that is open.
// __dirname output monitoring
console.log("__dirname: ", __dirname);