Node.js – Global Object and Modules

Node.js – Global Object and Modules

Global Object

In Node.js, a global object serves as a container for all global variables that are available in all modules of your application. Similar to the window object in browsers, the global object in Node.js provides access to various global functions and variables, such as console, setTimeout, and clearTimeout. To access the global object, you can use the keyword global.

Here are some Node.js components that are available globally:

  • global – A reference to the global object itself.
  • process – An object that provides information and control over the current Node.js process.
  • console – Provides access to standard printing functions.
  • Buffer – Class for working with binary data.
  • setImmediate(), clearImmediate() – Functions for asynchronous code execution.
  • setTimeout(), clearTimeout(), setInterval(), clearInterval() – Functions to manage code execution based on time.
  • module, exports, require() – Basic functions for managing modules.
  • __dirname, __filename – Contains paths to the directory and file of the current module.

NOTE:
Using a global object to share variables between modules is not a recommended practice due to potential problems with code maintenance and testing.

node.js

Module Wrapper Function

Modules in Node.js make it possible to isolate pieces of code into separate units. In Node.js, every module is automatically wrapped in a function. This wrapper function allows modules to have their own private scope, where the global object elements: exports, require, module, __filename, and __dirname now become its local function parameters. This means that when you write code in a Node.js module, you are actually writing code inside this wrapper function.

  • exports: This object is used to export methods or variables from a module. Any property or method added to the exports object becomes part of the module’s public interface.
  • require(): Function used to load other modules.
  • module: This object represents the current module and contains information about it, including the exports object.
  • __filename: Absolute path to the current module file. This means that it shows the full path from the root of the file system to the location where the file is located.

  • __dirname: The path to the directory of the current module.
EXPLANATION:

Think of modules as toolboxes. Each box (module) has its own tools (methods) that you can use when you have that box available. If you want your friend to use some of your tools, you put them in a “special” box that you send to him. In the Node.js world, it’s like using module.exports to “put the tools in the box” and require() to “open the box” someone sent you.

Now, imagine that each box automatically gets a separate bag when you ship it. In therein the bag you can put everything you need to make your tools work as they should, such as instructions or additional parts. In Node.js, it’s like “Module Wrapper Function”. When you write code in a module, Node.js automatically puts your code in that special function (bag) that gives you some useful things like exports, require, module, __filename, and __dirname, so that your code can work nicely with other parts of the application.

So, the “Module Wrapper Function” is like a “magic” bag that ensures that each box (module) has everything it needs to make the tools (code) inside work properly, even when they are sent somewhere far away

Imoport/Export Module

commonjs

To make a module available in other parts of the application, CommonJS syntax is used: module.exports for exporting, and require() for importing modules. You can read more about CommonJS in the article Modular Programming (External Syntax)

Example

Here is a module that exports one function:

And the following code shows how that function can be accessed and used in another file/module:

Built-in Modules in Node.js

Node.js comes with a rich set of built-in modules designed to make application development easier. These modules include a wide range of functionality, from file manipulation to HTTP server creation. Here are a few key ones:

  • Path Module: This module provides tools for working with file and directory paths.
    Example

  • OS Module: Enables interaction with the operating system.
  • FileSystem Module: Provides functions for reading, writing and manipulating files.
  • Events Module: Enables working with events through the implementation of the observer pattern.
  • Http Module: Allows creation of HTTP servers and clients.
    Example