Loading

Quipoin Menu

Learn • Practice • Grow

node-js / Path Module
tutorial

Path Module

Imagine you're giving directions to someone. You need to tell them the correct path – not just "go to the folder" but exactly how to get there. In Node.js, the **Path module** helps you work with file and directory paths in a reliable way that works on all operating systems.

What is the Path Module?

The Path module provides utilities for working with file and directory paths. It handles the differences between Windows (which uses backslashes ``) and Unix-like systems (which use forward slashes `/`) automatically.

Think of the Path module as your GPS for file systems – it knows how to navigate, join paths, and find your way regardless of which operating system you're on.

Importing the Path Module

The Path module is built-in, so you don't need to install anything. Just require it:
const path = require('path');

path.join() – Joining Path Segments

`path.join()` joins all given path segments together using the platform-specific separator and normalizes the resulting path.
const fullPath = path.join('users', 'john', 'documents', 'file.txt');
console.log(fullPath);
<!-- Windows: usersjohndocumentsfile.txt -->
<!-- Mac/Linux: users/john/documents/file.txt -->

<!-- Handles extra slashes automatically -->
const messy = path.join('/users/', '//john', 'documents');
console.log(messy); <!-- /users/john/documents (normalized) -->

path.resolve() – Resolving Paths

`path.resolve()` resolves a sequence of paths into an absolute path. It works like `cd` in terminal – starting from right to left until it creates an absolute path.
const absolutePath = path.resolve('users', 'john', 'file.txt');
console.log(absolutePath);
<!-- /current/working/directory/users/john/file.txt -->

<!-- Using with __dirname is very common -->
const configPath = path.resolve(__dirname, 'config', 'settings.json');

path.basename() – Getting the File Name

Returns the last portion of a path – usually the file name.
const filePath = '/users/john/documents/report.pdf';
console.log(path.basename(filePath)); <!-- report.pdf -->
console.log(path.basename(filePath, '.pdf')); <!-- report (without extension) -->

path.dirname() – Getting the Directory Name

Returns the directory part of a path.
const filePath = '/users/john/documents/report.pdf';
console.log(path.dirname(filePath)); <!-- /users/john/documents -->

path.extname() – Getting File Extension

Returns the extension of the path (including the dot).
console.log(path.extname('image.jpg')); <!-- .jpg -->
console.log(path.extname('archive.tar.gz')); <!-- .gz (not .tar.gz) -->
console.log(path.extname('file.')); <!-- . -->
console.log(path.extname('README')); <!-- '' (empty string) -->

path.parse() – Parsing a Path into Components

This is super useful – it breaks a path into its components:
const parsed = path.parse('/users/john/documents/report.pdf');
console.log(parsed);
<!-- {
<!--   root: '/', -->
<!--   dir: '/users/john/documents', -->
<!--   base: 'report.pdf', -->
<!--   name: 'report', -->
<!--   ext: '.pdf' -->
<!-- } -->

path.format() – The Opposite of parse()

Creates a path string from an object:
const pathObj = {
  dir: '/users/john/documents',
  name: 'report',
  ext: '.pdf'
};
console.log(path.format(pathObj)); <!-- /users/john/documents/report.pdf -->

Other Useful Path Methods

MethodDescription
`path.isAbsolute()`Checks if a path is absolute.
`path.relative()`Gets the relative path from one path to another.
`path.normalize()`Normalizes a path (removes `..`, `.`, extra slashes).
`path.sep`Platform-specific path separator (`` on Windows, `/` on POSIX).

Practical Example: Building File Paths
const path = require('path');

<!-- Build a path to a config file in the same directory -->
const configPath = path.join(__dirname, 'config', 'app-settings.json');
console.log('Config file:', configPath);

<!-- Get just the filename from a full path -->
const fileName = path.basename(configPath);
console.log('File name:', fileName);

<!-- Check if a path is absolute -->
console.log(path.isAbsolute('./file.txt')); <!-- false -->
console.log(path.isAbsolute('/home/file.txt')); <!-- true -->

Two Minute Drill

  • Path module helps work with file paths across different operating systems.
  • `path.join()` joins path segments safely.
  • `path.resolve()` creates absolute paths.
  • `path.basename()`, `path.dirname()`, `path.extname()` extract path components.
  • `path.parse()` breaks a path into an object with all components.
  • Always use path methods instead of string concatenation for paths.

Need more clarification?

Drop us an email at career@quipoinfotech.com