Module in Node.js
Module in Node.js
Node.js uses the Module architecture to simplify the creation of complex applications. The module is like libraries in C, C #, Java . Each module contains a set of functions that are related to an "object" of the Module. For example, http is a Module containing specific functions related to HTTP settings. Node.js provides several additional core modules to assist us with accessing files on the system, creating HTTP, TCP / UDP servers, and other useful small utility functions.
Before using the Module, you simply need to declare with the require () function, as follows:
var http = require ( "http" );
Node.js is just an environment, you have to do everything yourself!
require () is a function that returns a reference to a specific Module. In the case of the above code, we are declaring a reference to the http module and saving it to the http variable.
In the above code, they pass a parameter as the name of the Module. This tells the Node to find a Module named http in the node_modules directory of the application. If it doesn't, Node will continue to find that Module in the global node installation directory.
The command to check the global directory to install node_modules, open the CMD command line interface and type the following command:
npm root - g
Back to the problem, you can specify the file by passing the parameter as the relative path ./path/to/my/module.js or absolute /path/to/my/module.js
var myModule = require ( './myModule.js' )
In short, Module is code that is packaged together. The code in a Module is usually private - meaning functions, variables defined and accessed by the inside of the Module. But, you can point out the api are functions and / or variables to use outside the Module. By using an export object, see the following example:
var PI = Math . PI ; exports . dientich = function ( r ) { return PI * r * r ; }; exports . chuvi = function ( r ) { return 2 * PI * r ; };
The code above creates a PI variable and it is only accessible in the Module we are defining. By using exports to output two functions used outside the Module are dientich () and chuvi (). So, suppose we are writing code on the ./myModule.js file, the reference variable myModule can call dientich () and chuvi () functions.
Global Scope in Node.js
Node.js is a server-enabled environment that runs JavaScript on the server and runs on Google's V8 JavaScript engine. Thus, we should implement the code like when using client-side programming. For example, we should limit the use of global variables. However, if you want to use it, you can easily create a global variable by defining the variable name without the var keyword, as follows:
globalVariable = 1 ; globalFunction = function () { . };
Again, global variables should be limited to the maximum. Therefore, be careful and remember to use the var keyword to declare the variable.
According to Tutorialspoint
Previous article: Instructions for installing Node.js
Next lesson: Hello World program in Node.js
You should read it
May be interested
- How to check if the Windows PC has a Trusted Platform Module (TPM) chipthis guide will show you how to check if your windows pc has the trusted platform module (tpm) security hardware chip, as well as see the tpm details and version, if any.
- Function sleep () in Pythonmodule time in python provides some very useful functions to handle time-related tasks. one of the most commonly used functions is sleep ().
- Google launched the Developers Kit Module for Project Aragoogle has just officially released the first developers kit of project ara for developers. through this set of mdks, google wants to help developers get a grasp of what to do to create replaceable parts for the upcoming puzzle phone.
- Raspberry Pi platform first introduces 12 MP camera module with interchangeable lensa 12mp camera module has been released by the raspberry pi for diy enthusiasts.
- How to remove the Magisk module using TWRP Recoverya quick guide to uninstalling the magisk module without using magisk manager is also known as uninstalling the magisk module using twrp recovery or offline.
- What is VRM and how does it affect the performance of the processorthe voltage regulator module (voltage regulator module - vrm) is one of the important hardware components in the general structure of a motherboard system.
- Module in Pythonin this python lesson we will know how to create and import a module in python. we will learn a few ways to import, using custom modules as well as python's built-in modules, please watch.
- Warning of Critical Vulnerability Affecting Realtek Wi-Fi Modulesthrough these vulnerabilities, malicious actors can completely gain higher privileges on the target device.
- Apple puts cameras on Airpodsaccording to analyst ming-chi kuo, apple will mass produce airpods with an infrared camera module similar to the face id module on iphone and ipad in 2026.
- How to use AWS DynamoDB in a Node.js applicationone of the solutions that amazon web services (aws) offers is dynamodb, a tool that revolutionizes data management.