</>
Skip to content
Node.js lessons (14/44)

Node.js — Path

Import Path Module

const path = require('path');

Path Methods

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

// Resolve absolute path
path.resolve('file.txt')
// '/current/directory/file.txt'

// Get directory name
path.dirname('/users/john/file.txt')
// '/users/john'

// Get file name
path.basename('/users/john/file.txt')
// 'file.txt'

// Get extension
path.extname('file.txt')
// '.txt'

Platform-Specific Paths

// Windows: C:\users\john\file.txt
// Unix: /users/john/file.txt

path.sep          // Separator character
path.delimiter    // Path delimiter
path.win32        // Windows paths
path.posix        // Unix paths

Practical Example

const configPath = path.join(__dirname, 'config', 'app.json');
const templatePath = path.join(__dirname, 'templates', 'email.html');

Mini Practice

  1. Join multiple path segments
  2. Get file extension
  3. Resolve absolute paths
  4. Use __dirname for project paths

Up Next

Continue with OS — operating system information.

Related Topics

Frequently Asked Questions about Path

What is Path in Node.js?

Path is a fundamental concept in Node.js. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Path?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Path.

Why is Path important in Node.js?

Path is essential for Node.js development. Understanding this concept will help you write better code and solve real-world problems more effectively.