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.
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.
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