The fs.realPath() method is used to compute the canonical pathname of the given path. It does so by resolving the ., .. and the symbolic links in the path.
Syntax:
fs.realpath( path, options, callback )
Parameters: This method accept three parameters as mentioned above and described below:
- path: It holds the path of the directory that has to be resolved. It can be a String, Buffer or URL.
- options: It is an string or object that can be used to specify optional parameters that will affect the operation. It has one optional parameter:
- encoding: It is a string which defines the encoding of the resolved path.
- callback: It is the function that would be called when the method is executed.
- err: It is an error that would be thrown if the operation fails.
- resolvedPath: It is string or buffer that represents the resolved path.
Below examples illustrate the fs.realPath() method in Node.js:
Example 1: This example uses fs.realPath() method to get the canonical paths of the given path.
const fs = require('fs');
console.log("Current Directory Path:", __dirname);
path1 = __dirname + "\\..";
fs.realpath(path1, (error, resolvedPath) => {
if (error) {
console.log(error);
}
else {
console.log("One directory up resolved"
+ " path is: ", resolvedPath);
}
});
path2 = __dirname + "\\..\\..";
fs.realpath(path2, (error, resolvedPath) => {
if (error) {
console.log(error);
}
else {
console.log("Two directories up resolved"
+ " path is:", resolvedPath);
}
});
|
Output:
Current Directory Path: G:\tutorials\nodejs-fs-realPath
Two directories up resolved path is: G:\
One directory up resolved path is: G:\tutorials
Example 2: This example uses fs.realPath() method to demonstrate the different encoding type.
const fs = require('fs');
path = __dirname + "\\..";
fs.realpath(path, {encoding: "utf8"}, (error, resolvedPath) => {
if (error) {
console.log(error);
}
else {
console.log("The resolved path is:", resolvedPath);
}
});
fs.realpath(path, {encoding: "hex"}, (error, resolvedPath) => {
if (error) {
console.log(error);
}
else {
console.log("The resolved path is:", resolvedPath);
}
});
fs.realpath(path, {encoding: "base64"}, (error, resolvedPath) => {
if (error) {
console.log(error);
}
else {
console.log("The resolved path is:", resolvedPath);
}
});
|
Output:
The resolved path is: G:\tutorials
The resolved path is: 473a5c7475746f7269616c73
The resolved path is: RzpcdHV0b3JpYWxz
Reference: https://nodejs.org/api/fs.html#fs_fs_realpath_path_options_callback
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
12 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...