Node.js | Global Objects

Node.js is an open-source project that can be used for server-side scripting. Node.js Global Objects are the objects that are available in all modules. Global Objects are built-in objects that are part of the JavaScript and can be used directly in the application without importing any particular module. The Node.js Global Objects are listed below:

  1. Class: Buffer The Buffer class is an inbuilt globally accessible class that means it can be used without importing any module. The Buffer class is used to deal with binary data. Buffer class objects are used to represent binary data as a sequence of bytes.
  2. console: It is an inbuilt global object used to print to stdout and stderr.
  3. process: It is an inbuilt global object that is an instance of EventEmitter used to get information on current process. It can also be accessed using require() explicitly.
  4. global: It is a global namespace. Defining a variable within this namespace makes it globally accessible.
    var myvar

    It is a global scope when declared within the browser. However, any variable defined within a node.js file is accessible only within that file.

  5. setImmediate() method: It schedules the immediate execution of the callback. The callback functions are queued and executed in the order in which they are created. The callback queue is processed at every event loop iteration. If there is a timer queued inside the executing callback, the timer will not get triggered until the next event loop iteration.
  6. clearImmediate() method: It stops the immediate object returned by the setImmediate() method.
  7. setInterval() method: It executes the callback function at repeated intervals. If an interval is larger than 2147483647 or less than 1, the interval is set to 1. Non-integer delays are truncated to the nearest integer.
  8. clearInterval() method: It stops the interval object created by setInterval() method.
  9. setTimeout() method: It is a global function used to run a callback function after at least delay in milliseconds. Node.js does not guarantee the exact timing of when callbacks will fire but tries to maintain the timing as close as possible to the specified delay. Any delay larger than 2147483647 or less than 1, is set to 1 automatically. Non-integer delays are truncated to the nearest integer.
    function printHello() {
       console.log( "Hello, World!");
    }
    
    // Now call above function after 2 seconds
    var timeoutObj = setTimeout(printHello, 2000);
    

  10. clearTimeout() method: The clearTimeout() method is used to cancel or stop a timeout that was set with setTimeout() method. The timeoutObj is the object returned by setTimeout() method.
  11. queueMicrotask() method: A microtask is a short function that is executed after the callback function exits and only if the JavaScript execution stack is empty. The queueMicrotask() method is used to excute such functions after the callback function completes successfully. If the callback function does not return the control to other JavaScript code, the event loop runs all of the microtasks in the microtask queue. The microtask queue is processed multiple times per iteration of the event loop. If a microtask adds more microtasks to the queue then the newly-added microtasks execute before the next task is run. This is because the event loop keeps calling microtasks until there are none left in the queue.
  12. TextEncoder: It is an implementation of the WHATWG Encoding Standard TextEncoder API. All instances of TextEncoder are encoded in UTF-8 only.
  13. TextDecoder: It is an implementation of the WHATWG Encoding Standard TextDecoder API.
  14. Class: URL The URL class instance is a global object and is implemented by the following WHATWG URL Standard. The URL constructor creates a new URL object as shown below. /foo is the input and https://www.helloworld.og/ is the base value.
  15. URLSearchParams: URLSearchParams API is used to perform read and write operations on the query of a URL.
    const myURL = new URL('https://www.register.com/?name=gfg');
    
    // It prints gfg
    console.log(myURL.searchParams.get('name'));
    
    myURL.searchParams.append('name', 'xyz');
    
    // It prints https://www.register.com/?name=gfg&name=xyz
    console.log(myURL.href);
    
  16. WebAssembly: The global object that acts as a namespace for all W3C WebAssembly related functionality. WebAssembly is a low level Assembly-like language that can be run on modern browsers.

The following variables might appear to be global but actually exist only within the scope of some modules.

  1. require(id) method: It is used to import modules and returns an object of ‘any’ datatype.
    var express = require('express')
  2. exports: It is used to exports modules using module.exports.
  3. module: It is a reference to the current module and is not global rather local to each module. It is used to make a particular module available through require() in the application.
  4. __dirname: The output throws an error which proves that __dirname is not globally defined in node.js. It requires a script to give the desired output as __dirname is only defined in scripts.

    • Create a demo.js file
    • Paste the following code:
      console.log("__dirname : "+ __dirname);
    • Run the demo.js file

  5. __filename: The output throws an error which proves that __dirname is not globally defined in node.js. It requires a script to give the desired output as __filename is only defined in scripts.

    • Create a demo.js file
    • Paste the following code:
      console.log("__filename : "+ __filename);
    • Run the demo.js file

My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.