Why Not Just Use Strings?
Windows uses backslashes (C:\Users\...) while macOS/Linux use forward slashes (/home/user/...). Manually concatenating path strings with + breaks the moment your code runs on a different OS — the path module abstracts this away.
Joining Paths Safely
Try it yourself — edit and run
Console Output
Click “Run” to see the console output here.
Common path Methods
| Method | Purpose |
|---|---|
| path.join(...) | Combine path segments using the correct OS separator |
| path.resolve(...) | Resolve segments into an absolute path |
| path.basename(p) | The final segment (filename) of a path |
| path.extname(p) | The file extension |
| path.dirname(p) | The containing directory |
Extracting Path Information
Try it yourself — edit and run
Console Output
Click “Run” to see the console output here.
__dirname Equivalent in ES Modules
Try it yourself — edit and run
Console Output
Click “Run” to see the console output here.
Best Practice
Never manually concatenate path segments with + or template literals — always use path.join() or path.resolve(), which handle separators and normalization correctly on every operating system.