Path Module in Node.js

From the Node.js Core Modules cheat sheet ยท File System (fs) ยท verified Jul 2026

Path Module

Work with file and directory paths across platforms

javascript
const path = require('path');

// Join paths
path.join('/users', 'john', 'documents', 'file.txt');

// Get parts
path.dirname('/users/john/file.txt');  // /users/john
path.basename('/users/john/file.txt'); // file.txt
path.extname('file.txt');              // .txt

// Resolve absolute
path.resolve('file.txt');
๐ŸŸข Essential - Handle paths correctly on all OS
๐Ÿ’ก Always use path.join() not string concatenation
๐Ÿ“Œ __dirname is current file's directory
โšก path.resolve() creates absolute paths
โš ๏ธ Windows uses \ while Unix uses /
๐Ÿ”— Related: process.cwd() for working directory

More Node.js tasks

Back to the full Node.js Core Modules cheat sheet