How and When to Use "Nodejs __dirname"

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:

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.

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:

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:

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:

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:

  1. Dot Notation (./ and ../) refers to the directory of the called Node command.
  2. process.cwd() refers back to the directory of the called Node command.

For example:

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.

Alexandra Leslie

Alexandra Leslie’s interest in website administration was sparked in her teens, priming her for a fast-paced career in managing, building, and contributing to online brands, including HostingAdvice, Forbes, and the blogs of prominent hosting providers. She brings to the table firsthand experience in reviewing web hosts, perfecting website design, optimizing content, and walking site owners through the steps that add up to a successful online presence. Today, she combines her extensive writing experience with technical understanding to unpack some of the most complex topics that daunt novice website owners, as well as the subjects that excite veteran technologists within the HostingAdvice readership.