Warning: session_start(): open(/tmp/sess_e678ef18017e60fcad37de058793f170, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Get Absolute Path Of File From Relative Path In Node.js - LearnShareIT

How To Get Absolute Path Of File From Relative Path In Node.js

Get absolute path of file from relative path in Node.js

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:

Leave a Reply

Your email address will not be published. Required fields are marked *