Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between process.cwd() vs __dirname?

Tags:

node.js

What's the difference between

console.log(process.cwd()) 

and

console.log(__dirname); 

I've seen both used in similar contexts.

like image 603
helpermethod Avatar asked Mar 26 '12 14:03

helpermethod


People also ask

What is process cwd ()?

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.

What does __ Dirname mean in Node?

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

What is __ Dirname in Webpack?

The __dirname is set to / by webpack, that's why you end up with /views/index. html which is the root of your file system, that happens to be D:\ in your case. You can set node. dirname to false in your webpack config to not inject it and defer it to runtime.

Why __ Dirname is not defined?

The __dirname or __filename global variables are not available in ECMAScript module files. To solve the "__dirname is not defined in ES module scope" error, import and use the dirname() method from the path module. The dirname method takes a path as a parameter and returns the directory name of the path. Copied!


2 Answers

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

like image 200
Raynos Avatar answered Sep 19 '22 13:09

Raynos


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 example,

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.

like image 30
SAMUEL Avatar answered Sep 18 '22 13:09

SAMUEL