- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between process.cwd & _ _dirname in NodeJS
NodeJS is a JavaScript runtime environment that was built on top of Chrome's V8 engine. The traditional use of JavaScript is to be executed in browsers, but with Node.JS we can execute JavaScript other than browsers like servers, hardware devices, etc.
process.cwd()
The process object lies under the global object known as 'global'. This method provides information about the current process of Node.js. It also provides control over the same. cwd refers to the current working directory of the same. Therefore, process.cwd returns the working directory on which execution is taking place currently.
__dirname
This is a local module that will return the directory name of the current module. Also, it is a local module
process.cwd | __dirname |
---|---|
Returns name of the current working directory. | Returns the name of the directory containing the source code files. |
It is a global object inside Node. | It is a local object |
It depends on the command which is invoked. | It depends upon the current directory. |
process.cwd() vs __dirname
Example 1 (index.js)
// Logging process.cwd() output console.log("process.cwd(): ", process.cwd()); // Logging __dirname output console.log("__dirname: ", __dirname);
Output
C:\Users\tutorialsPoint\> node index.js.js process.cwd(): /home/node/demo __dirname: /home/node/demo
Example 2
Create the following files with the below folder structure
-- src/ --index.js --src2/ --index2.js
Code Snippet (index.js) −
// Read and execute the index2.js file require('./sub1/index2.js')
Code Snippet (index2.js) −
// Logging process.cwd() output console.log("process.cwd(): ", process.cwd()); // Logging __dirname output console.log("__dirname: ", __dirname);
Run the index2.js file using the following command.
node index2.js
Output
process cwd: C:\src __dirname: C:\src\src2
The above output shows that the directory of the file index2.js is at src/src2 whereas the current node process is running in src/ folder.
- Related Articles
- Difference between NodeJS and AngularJS
- Difference between NodeJS and ReactJS
- Difference between console.log and process.stdout.write in NodeJS
- Difference between Process and Thread
- Difference Between Product and Process
- Difference Between Program and Process
- Difference between Product and Process in Software development
- dirname()function in PHP
- What is the difference between process and program?
- Encrypt and Decrypt Data in NodeJS
- How to work with the dirname attribute in HTML?
- Introduction to ensureFileSync() in NodeJS
- Introduction to Sequelize in NodeJS
- Differentiate between process switch and mode switch in OS
- Differentiate between job costing and process costing
