Thursday, June 20, 2024

Difference Between __dirname and process.cwd() in Node.js

In this article we'll see the difference between __dirname and process.cwd() method in Node.js.

__dirname in Node.js

__dirname in Node.js is a local variable which stores the absolute path of the directory where the currently executing file (module) resides.

In Node.js, each file is treated as a module and before a module's code is executed, Node.js will wrap it with a function wrapper that looks like the following:

(function(exports, require, module, __filename, __dirname) {
  // Module code actually lives in here
}); 

That's why convenience variables __dirname and __filename, containing the module's absolute filename and directory path are local variables which are specific to the module.

process.cwd() in Node.js

The process.cwd() method returns the current working directory of the Node.js process (not the module).

With process.cwd() working directory depends on from where the node process starts whereas with __dirname working directory depends on the file which is currently executing.

__dirname Vs process.cwd()

Let's try to clear the difference between __dirname and process.cwd() using few examples.

Suppose we have a file pathdemos/pathdemo.js in project root directory.

pathdemos/pathdemo.js

function test(){
    console.log('in function test()');
    console.log('__dirname:', __dirname);

    console.log('process.cwd():', process.cwd())
}

test();

module.exports = {test};

On running this file-

D:\NETJS\NodeJS\nodews\pathdemos>node pathdemo.js

in function test()
__dirname: D:\NETJS\NetJS_2017\NodeJS\nodews\pathdemos
process.cwd(): D:\NETJS\NetJS_2017\NodeJS\nodews\pathdemos

As you can see node invokes pathdemo.js so process.cwd() returns the working directory as path to pathdemo.js. Currently executing file is also pathdemo.js so __dirname also returns the current directory as path to pathdemo.js.

Let's try to execute test() function from some other file. So, we'll remove the test() method execution from pathdemo.js and export the function.

pathdemos/pathdemo.js

function test(){
    console.log('in function test()');
    console.log('__dirname:', __dirname);

    console.log('process.cwd():', process.cwd())
}

module.exports = {test};

There is another file app.js residing in project root directory.

app.js

const p = require('./pathdemos/pathdemo');
p.test();

Check the output by running app.js file.

D:\NETJS\NodeJS\nodews>node app.js

in function test()
__dirname: D:\NETJS\NetJS_2017\NodeJS\nodews\pathdemos
process.cwd(): D:\NETJS\NetJS_2017\NodeJS\nodews

As you see now __dirname is path to pathdemo.js because test() function executes in pathdemo.js where as process.cwd() gives path to project root directory because node invokes app.js so that is the running process.

That's all for this topic Difference Between __dirname and process.cwd() in Node.js. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Node.js Tutorial Page


Related Topics

  1. Node.js REPL
  2. How to Setup a Node.js Project
  3. NodeJS Blocking Non-blocking Code
  4. Node.js path.basename() Method With Examples
  5. Appending a File in Node.js

You may also like-

  1. Synchronization in Java - Synchronized Method And Block
  2. Marker Interface in Java
  3. How to Create PDF From XML Using Apache FOP
  4. BeanPostProcessor in Spring Framework
  5. Angular One-Way Data Binding Using String Interpolation

No comments:

Post a Comment