Tutorial

How To Use __dirname in Node.js

Node.js

Introduction

__dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.

In this article, you will explore how to implement __dirname in your Node.js project.

Prerequisites

To complete this tutorial, you will need:

Structuring Your Directories

This tutorial will use the following sample directory structure to explore how __dirname works. To begin your Node.js project, let’s organize your directories and files:

node-app
  ├──index.js
  ├──public
  ├──src
  │  ├──helpers.js
  │  └──api
  │      └──controller.js
  ├──cronjobs
  │  ├──pictures
  │  └──hello.js
  └──package.json

You can use __dirname to check on which directories your files live:

controller.js
console.log(__dirname)      // "/Users/Sam/node-app/src/api"
console.log(process.cwd())  // "/Users/Sam/node-app"
hello.js
console.log(__dirname)     // "/Users/Sam/node-app/cronjobs"
console.log(process.cwd()) // "/Users/Sam/node-app"

Notice that __dirname has a different value depending on which file you console it out. The process.cwd() method also returns a value, but the project directory instead. The __dirname variable always returns the absolute path of where your files live.

Working With Directories

In this section, you will explore how to use __dirname to make new directories, point to them, as well as adding new files.

Making New Directories

To create a new directory in your index.js file, insert __dirname as the first argument to path.join() and the name of the new directory as the second:

index.js
const fs = require('fs');
const path = require('path');
const dirPath = path.join(__dirname, '/pictures');

fs.mkdirSync(dirPath); 

Now you’ve created a new directory, pictures, after calling on the mdirSync() method, which contains __dirname as the absolute path.

Pointing to Directories

Another unique feature is its ability to point to directories. In your index.js file, declare a variable and pass in the value of __dirname as the first argument in path.join(), and your directory containing static files as the second:

index.js
express.static(path.join(__dirname, '/public'));

Here, you’re telling Node.js to use __dirname to point to the public directory that contains static files.

Adding Files to a Directory

You may also add files to an existing directory. In your index.js file, declare a variable and include __dirname as the first argument and the file you want to add as the second:

index.js
const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, '/pictures');

fs.openSync(filePath, 'hello.jpeg');

Using the openSync() method will add the file if it does not exist within your directory.

Conclusion

Node.js provides a way for you to make and point to directories, and add files to existing directories with a modular environment variable.

For further reading, check out the Node.js documentation for __dirname, and the tutorial on using __dirname in the Express.js framework.

Creative Commons License