430

What's the difference between

console.log(process.cwd())

and

console.log(__dirname);

I've seen both used in similar contexts.

Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
helpermethod
  • 59,493
  • 71
  • 188
  • 276

4 Answers4

660

process.cwd() returns the current working directory,

i.e. the directory from which you invoked the node command.

__dirname returns the directory name of the directory containing the JavaScript source code file

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Raynos
  • 166,823
  • 56
  • 351
  • 396
  • 13
    Does this mean that `process.cwd()` is synonym to `.` for all cases except for `require()`? – Alexander Gonchiy Aug 29 '15 at 09:44
  • 18
    @AlexanderGonchiy correct, `.` is relative to `process.cwd()` (so synonymous), except for `require()` which works relative to current executing file. See [here](http://stackoverflow.com/a/16730379/927631) for more. – dwelle Nov 05 '15 at 17:53
  • 5
    Note that the current working directory can be changed at runtime using process.chdir, so it is not always the directory from which node was invoked. – masterxilo Jan 05 '20 at 16:03
161

As per node js doc process.cwd()

cwd is a method of global object process, returns a string value which is the current working directory of the Node.js process.

As per node js doc __dirname

The directory name of current script as a string value. __dirname is not actually a global but rather local to each module.

Let me explain with examples:

suppose we have a main.js file that resides inside C:/Project/main.js and running node main.js both these values return same file.

or simply with the following folder structure

Project 
├── main.js
└──lib
   └── script.js

main.js

console.log(process.cwd())
// C:\Project
console.log(__dirname)
// C:\Project
console.log(__dirname === process.cwd())
// true

suppose we have another file script.js files inside a sub directory of project, i.e. C:/Project/lib/script.js and running node main.js which require script.js

main.js

require('./lib/script.js')
console.log(process.cwd())
// C:\Project
console.log(__dirname)
// C:\Project
console.log(__dirname === process.cwd())
// true

script.js

console.log(process.cwd())
// C:\Project
console.log(__dirname)
// C:\Project\lib
console.log(__dirname === process.cwd())
// false

Simply it can be stated as:

process.cwd() returns the value of directory where we run the node process, whereas

__dirname returns the value of directory where the current running file resides.

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
SAMUEL
  • 8,098
  • 3
  • 42
  • 42
15

Knowing the scope of each can make things easier to remember.

process is node's global object, and .cwd() returns where node is running.

__dirname is module's property, and represents the file path of the module. In node, one module resides in one file.

Similarly, __filename is another module's property, which holds the file name of the module.

themefield
  • 3,847
  • 30
  • 32
8

$ find proj

proj
proj/src
proj/src/index.js

$ cat proj/src/index.js

console.log("process.cwd() = " + process.cwd());
console.log("__dirname = " + __dirname);

$ cd proj; node src/index.js

process.cwd() = /tmp/proj
__dirname = /tmp/proj/src
user1412192
  • 87
  • 1
  • 3