Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js __dirname Variable

  • Last Updated : 08 Apr, 2021

The __dirname string gives the directory path of the current module, this is also similar to that of path.dirname() of the filename.

Return Value : It returns the directory path of the current module.

Example 1: Let’s create a file main.js

main.js




import path from 'path';
const __dirname = path.resolve();
console.log(__dirname)


Output: Now run node main.js.

Example 2: Replicating __dirname with path.dirname().

Javascript




import path from 'path';
const __dirname = path.resolve();
const __filename = path.resolve();
console.log(path.dirname(__filename));


Output: Now run node main.js.

Reference:https://nodejs.org/docs/latest/api/globals.html#globals_dirname

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!