- __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.
Filename is D:\DemoProject\app.js Directory name is D:\DemoProject
Example 2: Determine the path of the present script while executing routes\user.js file.
Filename is D:\DemoProject\routes\app.js Directory name is D:\DemoProject\routes
- Nodejs | Automatic restart NodeJs server with nodemon
- Get Your System Information - Using Python Script
- How to get the size of screen, current web page and browser window using jQuery?
- Get the current URL using jQuery?
- How to get title of current HTML page using jQuery ?
- Get current timestamp using Python
- How to get the current URL using AngularJS ?
- Get name of current method being executed in Java
- How to get current function name in PHP?
- How to get the current date and time in seconds?
- How to Get the Current Working Directory in Golang?
- How to get the current weeknumber of the year?
- How to get current year in PHP ?
- How to Get Current time in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.