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.

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.
|
1 2 3 4 5 6 7 8 9 10 |
(function wrapperFunction(exports, require, module, __filename, __dirname) { // For example, let's define a function function someFunction() { console.log("Greetings from my function!"); } // We export the function so that it can be used in other modules module.exports = someFunction; })() |
- 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.
123exports.myFunction = function() {console.log('This is a function exported from the module');}; - require(): Function used to load other modules.
1const fs = require('fs'); - module: This object represents the current module and contains information about it, including the exports object.
12345module.exports = {myFunction: function() {console.log('This is a function exported from the module');}}; - __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.
1console.log(__filename); // Shows the full path to the current file - __dirname: The path to the directory of the current module.
1console.log(__dirname); // Shows the path to the directory of the current file
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

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:
|
1 2 3 4 5 6 7 |
// mojModul.js function pozdrav(naziv) { console.log(`Greetings, ${name}!`); } // We export the function module.exports = pozdrav; |
And the following code shows how that function can be accessed and used in another file/module:
|
1 2 3 4 5 |
// We import the greeting function from myModul.js const pozdrav = require('./mojModul'); // We use an imported function pozdrav('Svete'); |
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
12345678910111213const path = require('path');// Joining parts of the path into a wholeconst fullPath = path.join(__dirname, 'data', 'example.txt');console.log('Full file path:', fullPath);// Getting the last part of the path (filename)const fileName = path.basename(fullPath);console.log('File name:', fileName);// Dobijanje ekstenzije fileconst extension = path.extname(fileName);console.log('File extension:', extension); - 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
1234567891011const http = require('http');const server = http.createServer((req, res) => {res.writeHead(200, { 'Content-Type': 'text/plain' });res.end('Hello, World!');});const port = 3000;server.listen(port, () => {console.log(`Server je pokrenut na portu ${port}.`);});
