Path & OS Modules
Path & OS Modules
Node.js comes with built-in modules to help you interact with the file system and the operating system.
1. Path Module
The path module provides utilities for working with file and directory paths.
const path = require('path');
// Join pathsconst fullPath = path.join('/users', 'sj', 'documents', 'node.txt');console.log(fullPath); // outputs: /users/sj/documents/node.txt (on Mac/Linux)
// Get file extensionconsole.log(path.extname('index.html')); // .html
// Get directory nameconsole.log(path.dirname(fullPath)); // /users/sj/documents2. OS Module
The os module provides information about the computer’s operating system.
const os = require('os');
// Check system architectureconsole.log(os.arch()); // e.g., x64 or arm64
// Check available memory (in bytes)console.log(os.freemem());
// Get system uptime (in seconds)console.log(os.uptime());
// Get user infoconsole.log(os.userInfo());[!IMPORTANT] The
pathmodule is essential for making your code cross-platform, as it handles the difference between/(Mac/Linux) and\(Windows) automatically.