Path Module
Master the Node.js path module - join, resolve, parse, format, and manipulate file paths across platforms
Take this tutorial with you
The path Module
The path module provides utilities for working with file and directory paths. It handles the differences between operating systems automatically - Windows uses backslashes (\), while Linux and macOS use forward slashes (/).
const path = require('path');
Why use path? Manually concatenating paths with
+ '/' +is fragile. On Windows, it produces invalid paths. Thepathmodule handles all platforms correctly.
path.join() - Joining Paths
The most commonly used path method. It joins path segments using the correct platform separator and normalises the result:
const path = require('path');
// Platform-independent path construction
const fullPath = path.join('/users', 'abhishek', 'projects', 'app.js');
// Linux/macOS: /users/abhishek/projects/app.js
// Windows: \users\abhishek\projects\app.js
// Relative paths work too
console.log(path.join('/base', '..', 'other', 'file.txt'));
// /base/../other/file.txt → normalised to /other/file.txt
// Wait - path.join doesn't resolve '..' on its own.
// For normalised resolution, use path.resolve().
path.joinvs string concatenation:path.join('a', 'b', 'c')returnsa/b/c.'a' + '/' + 'b'works until someone passes an absolute path or a Windows path.
path.resolve() - Resolving to Absolute Paths
path.resolve resolves a sequence of paths to an absolute path. It processes segments from right to left, prepending the current working directory until an absolute path is found:
const path = require('path');
// If current working directory is /home/user/project:
path.resolve('dist', 'app.js');
// → /home/user/project/dist/app.js
path.resolve('/etc', 'config.json');
// → /etc/config.json (absolute path stops resolution)
path.resolve('src', '..', 'dist', 'app.js');
// → /home/user/project/dist/app.js (normalised)
// Common pattern: resolve relative to __dirname
const configPath = path.resolve(__dirname, '..', 'config', 'app.json');
// → /home/user/project/config/app.json (regardless of CWD)
join vs resolve
| Method | Behaviour | Use Case |
|---|---|---|
path.join() |
Concatenates segments with separator | Building relative paths |
path.resolve() |
Resolves to absolute path | Getting full paths |
const path = require('path');
// join - just concatenates
path.join('a', 'b', 'c'); // 'a/b/c'
path.join('/a', 'b', '/c'); // '/a/b/c' (extra /c is ignored)
// resolve - makes absolute
path.resolve('a', 'b', 'c'); // '/cwd/a/b/c'
path.resolve('/a', 'b', '/c'); // '/c' (last absolute wins)
path.basename() - Get the File Name
const path = require('path');
path.basename('/users/abhishek/app.js');
// 'app.js'
path.basename('/users/abhishek/app.js', '.js');
// 'app' - strips the extension
path.basename('/users/abhishek/');
// 'abhishek' - last directory
path.dirname() - Get the Directory
const path = require('path');
path.dirname('/users/abhishek/app.js');
// '/users/abhishek'
path.dirname('/users/abhishek/');
// '/users/abhishek'
// Equivalent to __dirname when used on __filename
console.log(path.dirname(__filename));
// Same as __dirname
path.extname() - Get the Extension
const path = require('path');
path.extname('index.html');
// '.html'
path.extname('archive.tar.gz');
// '.gz' - only the last extension
path.extname('.gitignore');
// '' - no extension (starts with dot)
path.extname('Makefile');
// '' - no extension
// Common use: switch behaviour based on file type
if (path.extname(filename) === '.json') {
return JSON.parse(content);
}
path.parse() - Parse a Full Path
Parses a path into its components - the most detailed method:
const path = require('path');
const parsed = path.parse('/users/abhishek/projects/app.js');
console.log(parsed);
Output on Linux:
{
root: '/',
dir: '/users/abhishek/projects',
base: 'app.js',
ext: '.js',
name: 'app'
}
Output on Windows:
{
root: 'C:\\',
dir: 'C:\\users\\abhishek\\projects',
base: 'app.js',
ext: '.js',
name: 'app'
}
path.format() - The Reverse of parse
const path = require('path');
const obj = {
dir: '/users/abhishek',
name: 'app',
ext: '.js',
};
console.log(path.format(obj));
// /users/abhishek/app.js
path.normalize() - Clean Up Paths
Normalises a path by resolving .. and . segments and cleaning duplicate separators:
const path = require('path');
path.normalize('/users//abhishek/./projects/../app.js');
// '/users/abhishek/app.js'
path.normalize('C:\\Users\\\\Abhishek\\.\\projects\\..\\app.js');
// 'C:\\Users\\Abhishek\\app.js'
path.relative() - Relative Path Between Two Paths
const path = require('path');
const from = '/users/abhishek/projects/app/src/index.js';
const to = '/users/abhishek/projects/app/dist/bundle.js';
console.log(path.relative(from, to));
// '../../dist/bundle.js'
// Useful for computing relative import paths
function relativeImport(fromFile, toFile) {
const rel = path.relative(path.dirname(fromFile), toFile);
return rel.startsWith('.') ? rel : './' + rel;
}
console.log(relativeImport(
'/src/app/models/user.ts',
'/src/app/services/user.ts'
));
// '../services/user'
path.isAbsolute() - Check if Path is Absolute
const path = require('path');
path.isAbsolute('/users/abhishek'); // true (Linux)
path.isAbsolute('C:\\Users'); // true (Windows)
path.isAbsolute('./relative'); // false
path.isAbsolute('relative'); // false
path.sep - Platform Separator
const path = require('path');
console.log(path.sep);
// Linux/macOS: '/'
// Windows: '\\'
// Useful for splitting paths
const parts = process.cwd().split(path.sep);
path.delimiter - PATH Delimiter
const path = require('path');
console.log(path.delimiter);
// Linux/macOS: ':'
// Windows: ';'
// Parsing PATH environment variable
const paths = process.env.PATH.split(path.delimiter);
console.log('Directories in PATH:', paths.length);
Common Patterns
Pattern 1: Safe File Extension Check
const path = require('path');
const ALLOWED_EXTENSIONS = ['.jpg', '.png', '.gif', '.webp'];
function isAllowedImage(filename) {
const ext = path.extname(filename).toLowerCase();
return ALLOWED_EXTENSIONS.includes(ext);
}
// Guards against: "image.jpg.exe" - extname only gets the LAST extension
// If you need to check the real file type, use file signatures (magic bytes)
Pattern 2: Getting All Ancestor Directories
const path = require('path');
function getAncestors(filePath) {
const ancestors = [];
let current = path.dirname(filePath);
while (current !== path.dirname(current)) {
ancestors.push(current);
current = path.dirname(current);
}
ancestors.push(current); // root
return ancestors;
}
console.log(getAncestors('/a/b/c/d/file.js'));
// [ '/a/b/c/d', '/a/b/c', '/a/b', '/a', '/' ]
Pattern 3: Finding Config Files Up the Tree
const path = require('path');
const fs = require('fs');
function findConfig(startDir = process.cwd()) {
let current = startDir;
while (true) {
const configPath = path.join(current, 'app.config.json');
if (fs.existsSync(configPath)) {
return configPath;
}
const parent = path.dirname(current);
if (parent === current) break; // Reached root
current = parent;
}
return null;
}
const configPath = findConfig();
console.log('Found config at:', configPath);
Key Takeaways
path.join()concatenates path segments - use it instead of string concatenationpath.resolve()returns an absolute path - use it with__dirnamefor reliable pathspath.parse()breaks a path intoroot,dir,base,name,extpath.basename()gets the file name; optionally strip the extensionpath.extname()returns only the last extension (tar.gz→.gz)- Always use
pathmethods instead of manual string manipulation - they handle cross-platform differences path.relative()computes the relative path between two absolute pathspath.normalize()cleans up redundant separators and./..segmentspath.sepis/on Linux/macOS and\on Windows- Use
path.resolve(__dirname, 'relative/path')to build absolute paths regardless of the current working directory
Keep learning
Related tutorials from across the library.
File System Module (fs)
Master Node.js file system operations - read/write files, work with directories, file stats, streams, and permissions
Web Development / Backend / Node.js / Modules
OS Module
Master the Node.js OS module - system information, memory, CPU, network interfaces, and platform utilities
Web Development / Backend / Node.js / Modules
Events Module
Deep dive into Node.js events module - EventEmitter methods, patterns, memory management, and real-world usage
Web Development / Backend / Node.js / Modules