The Wayback Machine - https://web.archive.org/web/20240118183627/https://www.geeksforgeeks.org/node-js-__dirname-variable/
Open In App
Related Articles

Node.js __dirname Variable

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 08 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials