What is the Purpose of __filename Variable in Node.js ?
Last Updated :
10 Jul, 2024
In Node.js, there are several global variables that are available in all modules. One such variable is __filename
. This article delves into the purpose, usage, and practical applications of the __filename
variable in Node.js.
What is __filename
?
The __filename
variable is a built-in global variable in Node.js that provides the absolute path of the file that is currently being executed. Unlike other global objects, __filename
is not a function, but a variable that holds a string value representing the file's full path.
Syntax:
console.log(__filename)
Return Value:
It returns the absolute filename of the current module.
Characteristics of __filename
:
- Global Scope:
__filename
is accessible from anywhere within the module without the need to import or require it. - Module-Specific: It always refers to the current file in which it is used, meaning that each module has its own
__filename
value. - Absolute Path: The value of
__filename
is the absolute path, meaning it includes the complete directory structure leading to the file.
Purpose of __filename
The primary purpose of __filename
is to provide the absolute path of the current module's file, which can be used for various tasks in Node.js applications, such as:
- File Operations: Reading or writing files relative to the current file's location.
- Dynamic Path Resolution: Constructing paths dynamically for use in importing modules or other file-based operations.
- Logging: Including the current file's path in logs or error messages for better debugging and tracing.
- Meta Programming: Obtaining metadata about the current file for tasks such as code generation or module introspection.
Installation Steps
Step 1: Make a folder structure for the project.
mkdir myapp
Step 2:Â Navigate to the project directory
cd myapp
Step 3: Initialize the NodeJs project inside the myapp folder.
npm init -y
Project Structure:

Example 1: Implementation to show the use of __filename variable in Node.js
Node
// app.js
console.log("GeeksforGeeks");
console.log("Name of the file which we"
+ " are currently executing is ");
console.log(__filename)
Step to Run Application:Â Run the application using the following command from the root directory of the project
node app.js

Example 2: In this example, we will use split function to split the directory returned by __filename.
Node
// app.js
console.log("GeeksforGeeks");
// To show to parts of file using filename.
const parts = __filename.split(/[\\/]/)
console.log( "This the all the parts "
+ "present in file :",parts);
Step to Run Application:Â Run the application using the following command from the root directory of the projectOutput:
node app.js

Example 3: In this example, we will simply display the filename not the directory. Firstly, we will get the directory using __filename then split it. Then we will print the last index of the split array.
Node
// app.js
console.log("GeeksforGeeks");
// To show exact name of the file.
const parts = __filename.split(/[\\/]/)
console.log( "FileName is : " + parts[parts.length-1]);
Step to Run Application:Â Run the application using the following command from the root directory of the project
node app.js

Similar Reads
What is the purpose of the 'node_modules' folder ? The node_modules folder is a directory in NodeJS projects that stores third-party libraries and dependencies. It's essential for managing dependencies, which are packages or modules that a NodeJS project relies on. When you install a package using npm or Yarn, these tools download the package along
5 min read
What is the purpose of module.exports in node.js ? The module.exports is actually a property of the module object in node.js. module. Exports is the object that is returned to the require() call. By module.exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() meth
3 min read
What does Double Underscore (__) in Front of a Variable in Node.js ? In the world of Node.js, variable naming conventions play a crucial role in code readability, maintainability, and the prevention of naming conflicts. One particular convention that often intrigues developers, especially those new to Node.js, is the use of a double underscore (__) in front of a vari
3 min read
What is the purpose of the process object in Node JS ? In NodeJS, the process object is a global object that provides access to information and control over the current NodeJS process. It offers various properties and methods to interact with the underlying operating system environment and manage the NodeJS process effectively. Let's explore the primary
2 min read
What is the purpose of process object in Node.js ? A process object is a global object available in the Node.js environment. It is globally available. We do not have to use the require() to import the module of the process object. The "process" object use to get current Node.js process details & also give control over that process. Properties of
2 min read