Node.js __dirname Variable
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
Contact Us