WikiGalaxy

Personalize

Utilizing Path and OS Modules in Node.js

Introduction to Node.js Modules

Node.js provides a rich set of built-in modules that equip developers with essential functionalities. Two of the most commonly used modules are Path and OS. These modules allow developers to interact with file paths and operating system-related utilities, respectively.

Path Module

The Path module provides utilities for working with file and directory paths. It is a core module in Node.js, which means you don't need to install it separately.

OS Module

The OS module provides a number of operating system-related utility methods and properties. It allows Node.js applications to interact with the operating system.

Using the Path Module

Joining Paths

The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.


const path = require('path');
const fullPath = path.join('/users', 'john', 'docs', 'file.txt');
console.log(fullPath); // Outputs: '/users/john/docs/file.txt'
        

Resolving Paths

The path.resolve() method resolves a sequence of paths or path segments into an absolute path.


const absolutePath = path.resolve('docs', 'file.txt');
console.log(absolutePath); // Outputs an absolute path to 'file.txt'
        

Extracting Directory Name

The path.dirname() method returns the directory name of a path.


const dirName = path.dirname('/users/john/docs/file.txt');
console.log(dirName); // Outputs: '/users/john/docs'
        

Extracting File Name

The path.basename() method returns the last portion of a path, similar to the Unix basename command.


const fileName = path.basename('/users/john/docs/file.txt');
console.log(fileName); // Outputs: 'file.txt'
        

Extracting File Extension

The path.extname() method returns the extension of the path, from the last occurrence of the period (.) character to end of string in the last portion of the path.


const fileExt = path.extname('/users/john/docs/file.txt');
console.log(fileExt); // Outputs: '.txt'
        

Using the OS Module

Getting OS Platform

The os.platform() method returns a string identifying the operating system platform for which the Node.js binary was compiled.


const os = require('os');
const platform = os.platform();
console.log(platform); // Outputs: 'darwin', 'win32', 'linux', etc.
        

Getting CPU Architecture

The os.arch() method returns a string identifying the operating system CPU architecture for which the Node.js binary was compiled.


const architecture = os.arch();
console.log(architecture); // Outputs: 'x64', 'arm', etc.
        

Getting Total Memory

The os.totalmem() method returns the total amount of system memory in bytes as an integer.


const totalMemory = os.totalmem();
console.log(totalMemory); // Outputs: total memory in bytes
        

Getting Free Memory

The os.freemem() method returns the amount of free system memory in bytes as an integer.


const freeMemory = os.freemem();
console.log(freeMemory); // Outputs: free memory in bytes
        

Getting Hostname

The os.hostname() method returns the hostname of the operating system as a string.


const hostname = os.hostname();
console.log(hostname); // Outputs: hostname of the OS
        
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025