Hello and welcome to my blog!!!
Below is a guide on using the fs modules realPath() method found in nodejs.
Few things to know about….
The fs.realPath() will return the canonical path which basically is the absolute path. The difference is the canonical path is the most accurate representation of the path.
For example an absolute path would be:
The canonical path would be the most accurate representation:
You will notice that the absolute path can be case insensitive and the canonical path is case sensitive to how the items are named on the system.
The fs.realpath() has 3 parameters the path, options (which is an optional parameter), and a callpack of two arguments (err, resolvedPath).
The signature for fs.realpath() is:
You probably can just ignore the 2nd parameter “options.” From my experimenting it can do 2 things pass a object which could be useful if you want to make changes to an object inside the function. Secondly, it can change the encoding of the output. The default is UTF8 which should be fine in most cases.
Here is a list of other encoding’s I found.
Lets Take a Look at an small program to demonstrate fs.realpath().
This program will look for a file called test.txt located in the above directory and return the canonical path.
The fs.realpath() will run asynchronously so you will need to put it inside a promise to get the correct results.