Top Stories

Ankaj Gupta
December 30, 2018

Require module in Node.js | Nodejs

Understanding require() in Node.js

The require() function allows you to include modules into your programs. You can import built-in Node.js modules, community-based modules (node_modules), and local modules.

How require() Works

The require() function returns an object, which references the value of module.exports for a given file. When invoked, it performs a sequence of tasks.

Important: require is a function that takes one argument called path in Node.js.

Module Resolution Order

The require() function searches for modules in the following order:

1. Built-in Node.js Modules

Core modules like fs, path, crypto, etc.

2. Modules in node_modules Folder

Community-installed packages like Express, Lodash, etc.

3. Local Modules (with Paths)

If the module name contains ./, /, or ../, it looks for the directory in the given path. Matches extensions: *.js, *.json, and *.node.

How to Use require() Function

Importing a Local Custom Module

Import modules from your project using relative or absolute paths:

const myLocalModule = require('./path/myLocalModules');

Importing a JSON File

Directly require JSON files to get parsed data:

const jsonData = require('./path/jsonFile.json');

Importing Built-in or npm Modules

Import Node.js built-in modules or packages from node_modules:

const crypto = require('crypto');        // Built-in
const express = require('express');      // From node_modules

Complete Example

Example demonstrating how to use the fs module to read a file:

const fs = require('fs');

fs.readFile('./myfile.txt', 'utf-8', (err, data) => {
    if(err) {
        throw err;
    }
    console.log('data: ', data);
});

Explanation: In this example, the path is ./myfile.txt. The require('fs') loads Node.js's built-in file system module.

Common require() Patterns

Path Prefixes

  • ./ - Current directory
  • ../ - Parent directory
  • / - Absolute path
  • No prefix - node_modules

File Extensions

  • .js - JavaScript files
  • .json - JSON files
  • .node - Compiled addons
  • Extensions optional for .js

Types of Modules

Module Type Example Use Case
Built-in require('fs') Core Node.js functionality
Local require('./myModule') Project-specific code
Third-party require('express') Community packages
JSON require('./config.json') Configuration data

Key Points

  • Module Caching: Modules are cached after the first require. Subsequent calls return the cached version
  • Circular Dependencies: Be careful with circular dependencies as they can cause unexpected behavior
  • Sync Operation: require() is synchronous and blocks execution until the module is loaded
  • module.exports: Use module.exports to export functionality from your modules

Summary

The require() function is fundamental to Node.js module system, allowing you to load built-in modules, third-party packages, local files, and JSON configurations seamlessly.

Understanding how require() resolves modules helps you write better organized, modular Node.js applications. Remember that module resolution follows a specific order, and files are cached after the first require.

Node.js
Read
Ankaj Gupta
December 27, 2018

What is module in Node.js?

A set of functions you include in your Node.js application, that is a module. Modules can be either single files or directories containing one or more files. Let's see in detail

Node.js Modules

Consider modules to be the same as JavaScript libraries. A module encapsulates code into a single unit of code.

Whenever you create any type of functions/objects in Node.js and you reuse these functions/objects, then this process you can call a module.

Note:

Whenever if we don't create any Node.js modules ourselves, then we already have modules at our disposal because the Node.js environment provides so many built-in modules for us.

# Roles inside of Node.js

The Module has two main roles inside of Node.js.

1. Foundation Role

The module's first role is to provide a foundation for all Node.js modules to build off. Each file is given a new example of this base module on load, which persists even after the file has run. This's why we are able to attach properties to "module.exports" and return them later as needed.

2. Loading Mechanism

The module's second big job is to handle Node.js module loading mechanism. The require() function that we use is actually an abstraction over module.require, which is itself just a simple wrapper around Module._load. This load method handles the actual loading of each file, and where we'll begin our journey.

1. Built-in Modules

Node.js has a set of built-in modules which you can use without any further installation. Built-in Modules are provided by the system.

  • Built-in modules are also called Core modules.

  • The Built-in modules are defined within Node.js source and are located in the "lib/" folder.

  • The Built-in modules have several modules compiled into the binary and load automatically when Node.js process starts.

  • Built-in modules are always preferentially loaded if their identifier is passed to require(). For example, require('http') will always return the built in HTTP module, even if there is a file by that name.

How to Include Built-in Modules

The module is implemented in the require('module') function, by using require() function you include the Built-in module in Node.js

Example: Include the "http" built-in module

var http = require('http');

Now your Node.js application has included and access to the HTTP module, and is able to create a server:

Example: Include and Use http Module

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello Node.js!');
}).listen(8080);

2. Custom Modules

Custom modules are created by user, by using exports keyword and use it in your application.

  • It is also called User defined modules, File modules or Own modules.

  • With the help of the exports keyword to make properties and methods available outside the module file.

How to create your Custom modules

You use exports keyword to create the Custom module in Node.js

Example: mymodule-file.js

Create a module that returns the value of add() function

exports.add = function(x, y) {
    return x + y;
}

How to Include Custom modules

require('module') function also used to include Custom module in Node.js

Example: index-file.js

Use the module "mymodule-file.js" in a Node.js file

var http = require('http');
var module_object = require('./mymodule-file'); // Here included mymodule-file.js
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write("mymodule-file output is : " + module_object.add(25, 60).toString());
    res.end();
}).listen(8080);

Summary

You've learned about Node.js modules - both built-in modules provided by the system and custom modules you can create using the exports keyword. Modules help organize and reuse code effectively in Node.js applications.

Node.js
Read
Ankaj Gupta
December 25, 2018

Node.js console based example

How To Write and Run Your First Program in Node.js

Learn to create, save, and run your first Node.js program using the console.log() function.

1

Create a new Node.js file

Create a new Node.js file and save it named console_first.js

File Name: console_first.js

console.log('Hello Node.js');
2

Open Command Prompt

Open Command Prompt or Node.js command prompt interface, depending on your operating system. For Windows users: Click on the start button and look for "Command Prompt" or simply write "cmd" in the search field.

Command Prompt search
Node.js search

Now that the command prompt is open, set your path. Here we have saved console_first.js file on the desktop.

Note: run the following code to navigate to desktop

cd desktop
3

Run the Program

After setting the path, run the following code to display output.

Note: For display output

node console_first.js

This code can be run on the command prompt or Node.js command prompt. You can see the output in the photos below.

a.) Command Prompt output

Command Prompt output

b.) Node.js Command Prompt output

Node.js console output

Success!

Here you called the console.log() function and displayed a message on the console. This is your first Node.js program!

Node.js
Read
Ankaj Gupta
December 25, 2018

How node.js is different from PHP and ASP ?

Node.js is different from PHP and ASP

A quick look at how request handling differs between Node.js (event-driven, non‑blocking) and traditional platforms like PHP/ASP (threaded, blocking).

N

How Node.js handles requests

  • Node.js sends the request to the server.

  • Picks new requests without waiting for the current one to finish (non‑blocking).

  • When a request completes, data is returned to the client via callbacks/promises.

  • Eliminates idle waiting; continues processing the next request immediately.

P

How PHP/ASP handle requests

  • Sends a request to the server.

  • Waits until the request is completed (blocking per request/thread).

  • Returns data only after completion.

  • Then sends the next request.

Key differences

  • Concurrency model: Node.js is event‑loop based; PHP/ASP rely on threads/process per request.

  • I/O strategy: Node.js non‑blocking I/O vs. typical blocking I/O stacks.

  • Resource usage: Node.js reuses a single event loop; PHP/ASP generally allocate per request.

  • Use cases: Node.js excels at real‑time, streaming, and chat; PHP/ASP suit request/response MVC apps.

Tip: In Node.js, prefer async/await with Promises to keep non‑blocking code readable and maintainable.

Node.js web development and designing
Read
Ankaj Gupta
December 22, 2018

What are the special features of Node.js

Features of Node.js

There are following some important features of node.js, that make Node.js the first choice of software architects.

1. Very Fast

Node.js is built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution, because of its non-blocking paradigm.

2. No Buffering

Node.js applications never buffer any data. These applications simply output the data in chunks, Node.js cuts down the overall processing time while uploading audio and video files.

3. Huge number of libraries

npm (Node Package Manager) with its simple structure helped the ecosystem of node.js proliferate and now the npm registry hosts almost 500.000 open source packages you can freely use.

4. Event Driven and Asynchronous

All APIs of Node.js library are asynchronous non-blocking, So a Node.js based server never waits for an API to return data. The server moves to the next API (Application Programming Interface) after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.

5. Single threaded

Node.js follows a single threaded model with event looping.

6. Highly Scalable

Node.js is highly scalable because event mechanism helps the server to respond in a non-blocking way.

7. License

Node.js is released under the MIT (Massachusetts Institute of Technology) license.

8. Open source

Node.js has cross-platform, open source community which has produced many excellent modules to add additional capabilities to Node.js applications.

Node.js
Read
Ankaj Gupta
December 21, 2018

What is Node.js | nodejs ?

Introduction of Node.js

Node.js (Node) is an open source, cross-platform runtime environment for developing server-side and networking applications. It allows you to run JavaScript on the server-side.

Node.js tutorials

Node.js is a JavaScript runtime built on Chrome's JavaScript V8 engine for easily building fast, scalable network applications.

Node.js is intended to run on a dedicated HTTP (HyperText Transfer Protocol) server and to employ a single thread with one process at a time.

Note : Node.js is written in JavaScript and It runs on various platforms like: Microsoft Windows, Linux, Unix and Mac OS X, etc.

History

Node.js was developed by Ryan Dahl in 2009, Node.js is a JavaScript runtime built on Chrome's JavaScript V8 engine.

Advantages of Node.js

  • Node.js can generate dynamic page content, It can create, open, delete, read, write, and close files on the server.

  • Node.js applications are event-based and run asynchronously. Node.js Code built on the Node platform does not follow the traditional model of receive, process, send, wait, receive.

  • Node.js uses an event-driven, according to its creator Ryan Dahl, is that it does not block input/output (I/O) model.

  • Node.js makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

  • Node.js is also provides a rich library of various JavaScript modules which simplifies the development of web applications using Node.

  • Node is useful for developing applications that require a persistent connection from the browser to the server and is often used for real-time applications such as: news, chat, feeds and web push notifications etc...

Node.js
Read
Ankaj Gupta
December 16, 2018

What is C Programming Language

The C Language is developed for creating system applications that directly interact with hardware devices such as kernels and device drivers etc...

Introduction of C Language

C is a general-purpose high-level programming language. C is a successor of Basic Combined Programming Language (CPL) called B language, developed in the 1960s at Cambridge University. It was developed for the Unix operating system.

c language

B was further modified by Dennis Ritchie in 1972 at (AT&T) Bell Laboratories in Murray Hill to develop the UNIX Operating System. This new language was called C language.

C language is also called mother language of all programming languages. It is used to develop system software and Operating Systems and many other programming languages were derived directly or indirectly from C programming concepts.

It is also known as:
  • Mother language
  • Procedure-oriented and General purpose programming language
  • C is a simple and structure oriented programming
  • C is a High level programming language

Procedural

Procedural means, C program is a set of functions; each function performs a specific task. In a C program, functions are called in sequence to make the program work as designed.

General-purpose

C language is designed for developing software that applies in a wide range of application domains. It is mainly used to design Operating Systems, Compilers, Database Languages, Interpreters, Utilities, Network Drivers, Assemblers, etc.

c language
Read
Ankaj Gupta
December 15, 2018

What are the applications of C programming?

Application of C Language

Application of C Programming are listed below, Let's see:

  • C programming language can be used to design Operating System, computer applications and Network Devices etc...

  • C Language is also used for Develop Desktop application and system software.

  • C language can be used to design the compilers

  • Almost every Device Drivers is written in C programming

  • It is used for developing verification software, test code, simulators etc... for various applications and hardware products.

  • It is used to develop application software like database and spread sheets etc...

  • One of the most popular database management systems is written in C programming language.

  • c language - MySQL
  • Both Unix and Linux were written in C. In fact, C was invented for the sole purpose of writing a cross platform version of Unix.

  • c language - Linux
  • MS Office was developed using assembler, then development moved to C, later, when C++ arose, everything new was done using C++ language

  • Major part of Web browser is written in C Language

c language
Read
Ankaj Gupta
December 04, 2018

What is Cryptography?

Introduction Of Cryptography

Cryptography is a means for implementing some security mechanisms. The term of 'Cryptography' is derived from two Greek words, namely crypto and graphy. In greek language crypto means secret and graphy means writing.

  • Cryptography is the science of secret writing that provides various techniques to protect information that is an unreadable format. This unreadable format only by the intended recipients.

  • It is the science of converting a message into a code from that hides the information contained in the message. We encrypt a message before its transmission so that an eavesdropped may not get the information contained in the message.

Plaintext : The original unencrypted message is called plaintext

Ciphertext : The encryption message is called ciphertext.

Cryptography technique and protocols:

Cryptography techniques and protocols are used in a wide range of application such as secure electronic transactions, se audio/video broadcasting and se video conferences.

  • In secure electronic transactions, cryptography techniques are used to protect E-mail messages, Credit-card information and other sensitive information.

  • In secure audio/video broadcasting the service provider sends the requested audio/video data to subscribers in a secure way. Only the authorized subscribers are allowed to views the multimedia data.

  • It is used in video conferences to allow multi-party communication, where one user speaks and the remaining users view the communicated data is a secure way.

  • These secure application are heavily dependent on various cryptography services namely confidentiality, authentication and data integrity.

Based on these cryptography services, the cryptography technique and protocols are classified into 4-main regions and are as follows:

  1. 1. Symmetric Encryption.

  2. 2. Asymmetric Encryption.

  3. 3. Data integrity Techniques.

  4. 4. Authentication Protocols.

1.) Symmetric Encryption

Symmetric encryption is an application technique is which identical cryptography key is used for both encrypting and decrypting the information. This key in practice must be secret between the sender and the receiver to maintain the secrecy of the information.

2.) Asymmetric Encryption

Asymmetric encryption is an encryption technique where two keys are used as a pair. Among these two keys, one key is used for encryption and the other key is used for decryption of information. In the pair of keys, if the sender uses any one key to encrypt a message, the receiver should use another key to decrypt the message.

3.) Data integrity Techniques

These techniques are used to protect information from alteration during the transmission. Data integrity techniques assure to maintain the accuracy and consistency of information over its entire life cycle.

4.) Authentication Protocols

These are designed based on the use of cryptographic techniques to authenticate the identity of the sender. These protocols allow only the valid users to access the resources located on a server.

Cryptography & Network Security
Read
Ankaj Gupta
September 23, 2018

Download free IDE softwares


Software Link
Read
Ankaj Gupta
September 20, 2018

Download Text editor Software

Code Editors for Developers

Popular editors to write and manage code efficiently. Choose your favorite and download.

Sublime Text

Sublime Text

Fast, lightweight editor with powerful multi‑cursor editing.

Download
Brackets

Brackets

Open‑source editor focused on web projects and live preview.

Download
Notepad++

Notepad++

Lightweight Windows editor with plugin ecosystem and tabs.

Download
Visual Studio

Visual Studio

Full‑featured IDE for .NET, C++, Python and more.

Download
VS

Visual Studio Code

Free, open‑source editor with extensions and debugging.

Download
A

Atom

Hackable editor built by GitHub, highly customizable.

Download
JetBrains

PyCharm

IDE for Python development with debugging and testing.

Download
V

Vim

Modal text editor with powerful keyboard‑driven editing.

Download
๐Ÿ˜

PhpStorm

IDE for PHP with smart coding assistance and refactoring.

Download
W

WebStorm

IDE for JavaScript, TypeScript, and web development.

Download
G

GNU Emacs

Extensible editor with Lisp‑based configuration system.

Download
Software Link
Read
Ankaj Gupta
September 18, 2018

Install PHP

Web Stack Installers

Download popular local server stacks for different platforms.

WampServer logo

WAMP for Windows

Windows + Apache + MySQL/MariaDB + PHP for local PHP development.

Download
XAMPP logo

XAMPP (Windows, macOS, Linux)

Cross‑platform stack: Apache, MySQL/MariaDB, PHP, Perl and more.

Download
LAMP illustration

LAMP for Linux

Linux + Apache + MySQL/MariaDB + PHP; common Linux web stack.

Download
MAMP logo

MAMP for macOS

macOS stack with Apache/Nginx, MySQL, and PHP for local development.

Download
Software Link
Read