Explore the Functionality of os, path, util, and events Modules
Wed Apr 23 2025 01:27:41 GMT+0000 (Coordinated Universal Time)
Saved by
@signup
✅ 1. os Module – System Info
const os = require('os');
console.log("Operating System:", os.type());
console.log("Free Memory:", os.freemem());
console.log("Home Directory:", os.homedir());
✅ 2. path Module – File & Directory Paths
const path = require('path');
const filePath = "/users/admin/project/index.js";
console.log("Directory Name:", path.dirname(filePath)); // /users/admin/project
console.log("Base Name:", path.basename(filePath)); // index.js
console.log("Extension:", path.extname(filePath)); // .js
✅ 3. util Module – Utility Functions
const util = require('util');
const greet = util.format("Hello %s, your score is %d", "Anita", 95);
console.log(greet); // Hello Anita, your score is 95
✅ 4. events Module – Event Emitter
const EventEmitter = require('events');
const emitter = new EventEmitter();
// Registering an event
emitter.on('greet', (name) => {
console.log(`Hello, ${name}!`);
});
// Emitting the event
emitter.emit('greet', 'Rahul'); // Output: Hello, Rahul!
content_copyCOPY
https://chatgpt.com/share/6806ee0c-b024-800e-83c3-9fa4fe10af07
Comments