← Back to Node.js Course | Chapter 2: Core Modules | Lesson 2 of 7

path मॉड्यूल

path module हर operating system पर सही तरीके से file paths बनाता और तोड़ता है।

In this page:

  1. path Module
Syntax
javascript
const path = require('path');
path.join(segment1, segment2)
path.resolve(segment)
path.basename(filePath)
path.extname(filePath)

path Module

path.join सही separator से segments जोड़ता है, path.resolve एक absolute path बनाता है, और path.basename, dirname, और extname paths अलग करते हैं। path इस्तेमाल करना slashes वाली hand-built strings से आने वाली bugs से बचाता है।

Note: अपने script के बगल की files reference करने के लिए path.join(__dirname, "file") इस्तेमाल करें।

उदाहरण: path module

javascript
const path = require("path");
console.log(path.join("a", "b", "..", "c.txt"));
console.log(path.basename("/tmp/app/index.js"));
console.log(path.extname("photo.final.png"));
console.log(path.dirname("/tmp/app/index.js"));
console.log(path.parse("/home/u/file.txt").name);

// Output:
// a/c.txt
// index.js
// .png
// /tmp/app
// file

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. plus signs से paths concatenate करना
  2. backslashes hard-code करना
  3. join को resolve से confuse करना
चैप्टर सारांश
  • join segments combine करता है
  • resolve absolute paths बनाता है
  • basename, dirname, extname paths split करते हैं
  • Operating systems के आर-पार काम करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.