How to Get the Path of Current Script using Node.js ? Last Updated : 13 Sep, 2024 Comments Improve Suggest changes Like Article Like Report In Node JS, getting the path of the current script is useful for file operations, logging, and configuration. It allows you to access related files or directories reliably, regardless of where your Node.js process was started.ApproachTo get the path of the present script in node.js we will be using __dirname for the directory path and __filename scope variables for the full path. __dirname: It returns the directory name of the current module in which the current script is located.__filename: It returns the file name of the current module. This is the current module file's absolute path with symlinks (symbolic links) resolved. Let's Consider the below file structure of the project:Below examples illustrate the use of __dirname and __filename module scope variable in node.js: Example 1: Determine the path of the present script while executing app.js file. JavaScript // Filename -app.js // Node.js program to demonstrates how to // get the current path of script // To get the filename console.log(`Filename is ${__filename}`); // To get the directory name console.log(`Directory name is ${__dirname}`); Output:Filename is D:\DemoProject\app.jsDirectory name is D:\DemoProjectExample 2: Determine the path of the present script while executing routes\user.js file. JavaScript // Filename - user.js // Node.js program to demonstrates how to // get the current path of script // To get the filename console.log(`Filename is ${__filename}`); // To get the directory name console.log(`Directory name is ${__dirname}`); Output:Filename is D:\DemoProject\routes\app.jsDirectory name is D:\DemoProject\routesConclusionWe can get the full path of current script file using __filename and the directory path using __directory variables. These appraches provide an effective way to access the the path of the current script and ensures reliable file and directory handling and consistency across different runtime environments. Comment More infoAdvertise with us Next Article How to Get the Path of Current Script using Node.js ? R Rajnis09 Follow Improve Article Tags : Technical Scripter Web Technologies Node.js Technical Scripter 2019 Node.js-Misc +1 More Similar Reads How to get the path of current script in R ? In this article, we will see how to determine the path of the current script in R Programming Language. Method 1: Traditional method If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will 2 min read How to Get the Current URL using JavaScript? Here are two different methods to get the current URL in JavaScript.1. Using Document.URL PropertyThe DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://).Syntaxdocument.URLReturn Val 1 min read How to get the current pathname in the app directory of Next.js? NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to get the current pathname in the ap 3 min read How To Get Current Route In Next.js? Next.js is a popular React framework that makes it easy to build server-side rendered and static web applications. One common task in web development is determining the current route or URL of the page. In Next.js, this can be done using the built-in useRouter hook. This article will guide you throu 3 min read How to Change npm start Script of Node.js ? In Node.js, npm (Node Package Manager) provides a convenient way to manage project scripts through the scripts field in the package.json file. By default, the npm start command is used to start your Node.js application. However, you might need to customize this command to suit specific requirements, 3 min read How to Serve Static Content using Node.js ? Accessing static files are very useful when you want to put your static content accessible to the server for usage. To serve static files such as images, CSS files, and JavaScript files, etc we use the built-in middleware in node.js i.e. express.static. Setting up static middleware: You need to crea 2 min read How to get the file name from full path using JavaScript ? Given a file name that contains the file path also, the task is to get the file name from the full path. There are a few methods to solve this problem which are listed below: JavaScript replace() method: This method searches a string for a defined value, or a regular expression, and returns a new st 2 min read How to print command line arguments passed to the script in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In Node.js if you want to print the command line arguments then we can access 2 min read How to check the given path is file or directory in node.js ? Sometimes there is a need to check whether the given path is a file or directory so that different operations can be performed based on the result. For instance, to log the information of the directory and file separately. In Node.js, file handling is handled by the fs module. You can read more abo 4 min read How to Show the Line which Cause the Error in Node.js ? Debugging is a critical part of software development. When an error occurs in a Node.js application, understanding exactly where it happened is essential for diagnosing and fixing the problem. Node.js provides several ways to pinpoint the line of code that caused an error. This article explores thes 4 min read Like