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 serves as Tech Vertical Manager of Digital Brands Inc, spearheading the charge to deliver technical expertise and thought leadership to our rapidly growing audience of developers, engineers, and website owners here at HostingAdvice. You'll find her engaging with leaders in web hosting and tangentially related industries, enjoying honest discussions of their cutting-edge technologies. She loves getting her hands dirty with comprehensive reviews of popular hosting platforms and services, and she's a die-hard WordPress fan. Alexandra leads the HostingAdvice team with a passion for translating technical jargon into digestible action items anyone can use to build, monetize, and scale a web presence.