Sometimes when working in Node, you want to find the directory of the file you’re currently executing or the directory from which the Node command was called. This can be useful when requiring other files, reading or writing logs, and executing various filesystem-level functions.
There are at least three ways to refer to the current directory in Node:
- __dirname is the most common method. It gives you the path of the currently running file.
- process.cwd() is useful in special cases. It returns the directory from which you ran Node.
- Dot Notation (./ and ../) is useful for relative paths similar to the command line.
It’s important to note the differences in each method, and we’ll cover how and when to use __dirname in Node.js using examples below.
Examples of Finding Node.js Paths Using __dirname
The global variable __dirname, which is the same as the path.dirname() function of the __filename, will give you the directory in which the executing file is located.
Let’s walk through a few hypotheticals to illustrate how __dirname and process.cwd() function in different directories of your application. We’ll create a simple tree structure for our examples next.
Creating a Directory Tree Structure for Our Examples
You can copy the lines below into your terminal and hit ENTER:
mkdir -p /tmp/myapp/subdir cd /tmp/myapp echo " console.log('__dirname (the directory of the script file): ', __dirname); console.log('process.cwd() (the directory from which the script file was called): ', process.cwd()); " > app.js cp app.js subdir/ Now we have two identical app.js scripts in two directories: one in the root directory of “myapp” and another in a subdirectory called subdir.
myapp/ app.js subdir/ app.js
Let’s see what output we get when we run the app.js script at different locations.
Example 1: In the Root Directory
Running root directory app.js from the root directory, we get:
$ cd /tmp/myapp $ node app.js __dirname (the directory of the script file): /tmp/myapp process.cwd() (the directory from which the script file was called): /tmp/myapp
You’ll notice there is no difference between the two methods here.
Example 2: In a Buried Directory
Running subdirectory app.js from the subdirectory, we get:
$ cd subdir/ $ node app.js __dirname (the directory of the script file): /tmp/myapp/subdir process.cwd() (the directory from which the script file was called): /tmp/myapp/subdir
Again, we see no difference.
Example 3: Executing a Script Within A Directory Other Than The Current Directory
Running the root directory app.js from the subdirectory, we get:
$ node ../app.js __dirname (the directory of the script file): /tmp/myapp process.cwd() (the directory from which the script file was called): /tmp/myapp/subdir
Here, we see __dirname is different from process.cwd(), because __dirname uses the location of the executing script file, which is in a directory other than the current directory.
Bonus: 2 Other Ways to Refer to File Directories in Node.js
As I referenced earlier, there are two other ways of referring to directories in Node:
- Dot Notation (./ and ../) refers to the directory of the called Node command.
- process.cwd() refers back to the directory of the called Node command.
For example:
var fs = require('fs'); console.log(fs.existsSync('./app.js')); // This prints "true" if you are in the myapp directory Note: Calling process.cwd() is the same as using the dot notion (./), except when using require(), which always works relative to the currently executing file.
Also, on Microsoft Windows computers, the file paths are constructed with backslashes instead of forward slashes. So, for portability reasons, we recommend you use the path module to construct relative file paths.
