Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
There are basically two ways to reference the current folder in a Node.js script:
./__dirname
Along with
./, there is../, which points to the parent folder. They behave in the same way.
There is a big difference between the two.
Using __dirname in a Node script will return the path of the folder where the current JavaScript file resides.
Using ./ will give you the current working directory. It will return the same result as calling process.cwd().
Initially the current working directory is the path of the folder where you ran the node command, but that can be changed during the execution of your script, by using the process.chdir() API.
The only place where ./ refers to the current file’s directory is inside a require() call. There, ./ is resolved relative to the file that contains the require().