Node.js fs.realpathSync() Method Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The fs.realpathSync() method is used to synchronously compute the canonical pathname of a given path. It does so by resolving the ., .. and the symbolic links in the path and returning the resolved path.Syntax: fs.realpathSync( path, options ) Parameters: This method accept two 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. Returns: It returns a String or a Buffer that represents the resolved path.Below examples illustrate the fs.realpathSync() method in Node.js:Example 1: javascript // Node.js program to demonstrate the // fs.realpathSync() method // Import the filesystem module const fs = require('fs'); console.log("Current Directory Path:", __dirname); // Finding the canonical path // one directory up path1 = __dirname + "\\.."; resolvedPath = fs.realpathSync(path1); console.log("One directory up resolved path is: ", resolvedPath); // Finding the canonical path // two directories up path2 = __dirname + "\\..\\.."; resolvedPath = fs.realpathSync(path2); console.log("Two directories up resolved path is: ", resolvedPath); Output: Current Directory Path: G:\tutorials\nodejs-fs-realPathSync One directory up resolved path is: G:\tutorials Two directories up resolved path is: G:\ Example 2: javascript // Node.js program to demonstrate the // fs.realpathSync() method // Import the filesystem module const fs = require('fs'); path = __dirname + "\\.."; // Getting the canonical path is utf8 encoding resolvedPath = fs.realpathSync(path, { encoding: "utf8" }); console.log("The resolved path is: ", resolvedPath); // Getting the canonical path is hex encoding resolvedPath = fs.realpathSync(path, { encoding: "hex" }); console.log("The resolved path is: ", resolvedPath); // Getting the canonical path is base64 encoding resolvedPath = fs.realpathSync(path, { encoding: "base64" }); 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_realpathsync_path_options Comment More info S sayantanm19 Follow Improve Article Tags : Web Technologies Node.js Node.js-fs-module Explore Node.js Tutorial 4 min read Introduction & Installation NodeJS Introduction 3 min read Node.js Roadmap: A Complete Guide 6 min read How to Install Node.js on Linux 6 min read How to Install Node.js on Windows 5 min read How to Install NodeJS on MacOS 6 min read Node.js vs Browser - Top Differences That Every Developer Should Know 6 min read NodeJS REPL (READ, EVAL, PRINT, LOOP) 4 min read Explain V8 engine in Node.js 7 min read Node.js Web Application Architecture 3 min read NodeJS Event Loop 5 min read Node.js Modules , Buffer & StreamsNodeJS Modules 5 min read What are Buffers in Node.js ? 4 min read Node.js Streams 4 min read Node.js Asynchronous ProgrammingAsync Await in Node.js 3 min read Promises in NodeJS 7 min read How to Handle Errors in Node.js ? 4 min read Exception Handling in Node.js 3 min read Node.js NPMNodeJS NPM 6 min read Steps to Create and Publish NPM packages 7 min read Introduction to NPM scripts 2 min read Node.js package.json 4 min read What is package-lock.json ? 3 min read Node.js Deployments & CommunicationNode Debugging 2 min read How to Perform Testing in Node.js ? 2 min read Unit Testing of Node.js Application 5 min read NODE_ENV Variables and How to Use Them ? 2 min read Difference Between Development and Production in Node.js 3 min read Best Security Practices in Node.js 4 min read Deploying Node.js Applications 5 min read How to Build a Microservices Architecture with NodeJS 3 min read Node.js with WebAssembly 3 min read Resources & ToolsNode.js Web Server 6 min read Node Exercises, Practice Questions and Solutions 4 min read Node.js Projects 9 min read NodeJS Interview Questions and Answers 15+ min read Like