I am Hack Sparrow
Captain of the Internets.

Understanding directory references in Node.js

So, there are three different ways to refer to directories in Node.js; namely - dot notation (./ and ../), __dirname, and process.cwd(). While all of them might seem to refer to the same thing, they can become a source of confusion and bugs, if they are not understood well. Let's examine how they work.

Create a directory named base with the following tree under it:

.
├── app.js
└── /one
├── one.js
└── /two
└── two.js

The content for app.js

var fs = require('fs');
console.log('------ base-app ------');
console.log('./app.js: ', fs.existsSync('./app.js'));
console.log('__dirname: ', __dirname);
console.log('process.cwd(): ', process.cwd());

require('./one/one.js');

The content for one.js

var fs = require('fs');
console.log('------ mod-one ------');
console.log('./one.js: ', fs.existsSync('./one.js'));
console.log('__dirname: ', __dirname);
console.log('process.cwd(): ', process.cwd());

require('./two/two.js');

The content for two.js

var fs = require('fs');
console.log('------ mod-two ------');
console.log('./two.js: ', fs.existsSync('./two.js'));
console.log('__dirname: ', __dirname);
console.log('process.cwd(): ', process.cwd());

Execute the files in each directory, and observe their output carefully.

In the directory base:

$ node app.js

------ base-app ------
./app.js: true
__dirname: /Projects/base
process.cwd(): /Projects/base
------ mod-one ------
./one.js: false
__dirname: /Projects/base/one
process.cwd(): /Projects/base
------ mod-two ------
./two.js: false
__dirname: /Projects/base/one/two
process.cwd(): /Projects/base

In the directory one:

$ node one.js

------ mod-one ------
./one.js: true
__dirname: /Projects/base/one
process.cwd(): /Projects/base/one
------ mod-two ------
./two.js: false
__dirname: /Projects/base/one/two
process.cwd(): /Projects/base/one

In the directory two:

$ node two.js

------ mod-two ------
./two.js: true
__dirname: /Projects/base/one/two
process.cwd(): /Projects/base/one/two

Now, run one.js from the directory two:

$ node ../one.js

------ mod-one ------
./one.js: false
__dirname: /Projects/base/one
process.cwd(): /Projects/base/one/two
------ mod-two ------
./two.js: true
__dirname: /Projects/base/one/two
process.cwd(): /Projects/base/one/two

Did you notice anything noteworthy?

./ and process.cwd() refers to the directory on which the node command was called. It does not refer to the directory of the file being executed.

__dirname refers to the directory where the file being executed resides.

So be careful, the next time you use ./, process.cwd(), or __dirname. Make sure what you are using is exactly what you want.

2 Responses to “Understanding directory references in Node.js”

  1. Liran Tal says:

    I guess that for two.js you were actually intending to load two.js, such as:
    console.log(‘./two.js: ‘, fs.existsSync(‘./two.js’));

  2. Captain says:

    Ah, yes. I had pasted the wrong content. Corrected. Thanks!

Make a Comment