Path Module and Path global constant
What is Path module?
In Node.js, the path module is a built-in core module that provides utilities for working with file and directory paths.
What is Global constant?
In Node.js, a global constant is a value that is always available in your code without requiring an import or declaration.
Example of both:
const path = require("path");
const file = "text/peter.txt";
console.log(path.extname(file)); // Output: .txt
console.log(path.dirname(file)); // Output: text
console.log(path.basename(file)); // Output: peter.txt
console.log(path.resolve("text", "peter.text")); // Output: absolute path to text/peter.text
console.log(path.isAbsolute(file)); // Output: false (if the path is not absolute)
This are the global constants in Node.js
console.log(__dirname); // Output: current directory path
console.log(__filename); // Output: current file path
Why do we need global constants?
This is very helpful when you're working with file systems (like reading HTML or CSS files), especially when your project is running on different machines or environments. It ensures your file paths are always accurate.