There are several ways to help you get absolute path of file from relative path in Node.js. These include using __dirname and __filename variables or the resolve() method. Check out this article for further information.
Get absolute path of file from relative path in Node.js
Here are two methods that we introduce in this tutorial. Let’s see what those ways are now.
Using the resolve() method
The resolve() method will return an absolute path from a relative one. This method takes one or more path segments and then returns the absolute path.
Syntax:
path.resolve( [...paths] )
For illustration, take a look at the example below:
const {resolve} = require('path'); //Resolving a path-segment const absolutePath = resolve('./test.js'); console.log('Absolute path: ' + absolutePath);
Output:
Absolute path: C:\Users\Admin\Desktop\Nodejs\test.js
In the above example, we pass a string ‘./test.js‘ to the resolve() method. The result returned is a string with the absolute path – A combination of the current directory path you are working in and the strings declared in the resolve() method, from left to right.
In case you do not pass an argument or an empty string to the resolve() method, the return result will be the absolute path to the current directory.
Note: If you use ES6 Modules syntax, replace the require statement with an import statement as follows
import { resolve } from 'path'
Using __dirname and __filename variables
In the same way, you can also use the __dirname and __filename variables to get the absolute path of a file from the relative path.
Suppose here is the structure of your project:
Nodejs
| __ component
| | __ firstComponent.js
| __ index.js
The following example will give you a better understanding of how to use them. In firstComponent.js, execute the commands below:
//directory name of the current module console.log('Directory name: ' + __dirname); //absolute path of the current module console.log('Absolute path: ' + __filename);
Output:
Directory name: C:\Users\Admin\Desktop\Nodejs\component
Absolute path: C:\Users\Admin\Desktop\Nodejs\component\firstComponent.js
In Node.js, __dirname represents the directory where the code is being executed. Besides, __filename represents the filename of the code being executed. This is the absolute path to the file containing this code.
Summary
To sum up, you have gone through some methods to get absolute path of file from relative path in Node.js. You can use one of the above methods depending on personal preference. However, we recommend using the resolve() method as the most comprehensive approach. Thanks for your interest.
Maybe you are interested:
- Npm WARN old lockfile The package-lock.json file was created with an old version of npm
- [ERR_UNSUPPORTED_DIR_IMPORT] in Node.js
- Replace a String in a File using Node.js

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js