Node.js – What’s the difference between process.cwd() vs __dirname

node.js

What's the difference between

console.log(process.cwd())

and

console.log(__dirname);

I've seen both used in similar contexts.

Best Answer

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