In Node.js, you can get the absolute path of a file or directory using the path
module. Here's a simple example:
const path = require('path');
// Get the absolute path of a file or directory
const relativePath = 'your_relative_path_here';
const absolutePath = path.resolve(relativePath);
console.log('Absolute Path:', absolutePath);
Replace 'your_relative_path_here'
with the actual relative path you want to convert to an absolute path. The path.resolve()
method finds the path connected to the current working directory and gives you the absolute path.
If you want to get the absolute path of the current working directory, you can use __dirname
:
const path = require('path');
// Get the absolute path of the current working directory
const absolutePath = __dirname;
console.log('Absolute Path:', absolutePath);
The __dirname
variable contains the absolute path of the directory where the current JavaScript file resides.