Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Difference between __dirname and ./ in Node.js
Next article icon

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

Last Updated : 16 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Node.js, both process.cwd() and __dirname are used to get the directory paths, but they serve different purposes. The key difference is that process.cwd() returns the current working directory from which the Node.js process was started, while __dirname returns the directory name of the current module (the file in which the code is running).

This means that process.cwd() can change based on where the process is run, whereas __dirname is constant and points to the location of the script file. In this article, we’ll explore the differences between process.cwd() and __dirname in more detail.

What is process.cwd()?

process.cwd() stands for "current working directory". It returns the directory from which the Node.js process was started. In other words, process.cwd() gives you the directory where the Node.js command was executed, regardless of where the script is located.

Syntax:

process.cwd();

What is __dirname?

__dirname is a built-in variable in Node.js that gives the absolute path of the directory containing the current JavaScript file (the file in which __dirname is used). Unlike process.cwd(), which is dependent on where the process is started, __dirname always points to the directory of the script file.

Now Let's see a few examples demonstrating both these.

Example 1:

index.js
// Logging process.cwd() output console.log("process.cwd(): ", process.cwd());  // Logging __dirname output console.log("__dirname: ", __dirname); 


Output:

process.cwd(): C:\src
__dirname: C:\src

In this case node process is running in the current directory

Example 2: Create the following 2 files with the following folder structure:

src/
___ index.js
___ src2/
___index2.js

FilePath: src/index.js

index.js
// Read and execute the index2.js file  require('./sub1/index2.js') 

FilePath: src/src2/index2.js

index2.js
// Logging process.cwd() output console.log("process.cwd(): ", process.cwd());  // Logging __dirname output console.log("__dirname: ", __dirname); 


Output:

process cwd:  C:\src
__dirname: C:\src\src2

This shows that the current node process was running in src/ folder i.e node index.js and the directory of the file index2.js was at src/src2.

Difference Between process.cwd() and __dirname

Featureprocess.cwd()__dirname
DefinitionReturns the current working directory where the Node.js process was started.Returns the directory name of the current module (file).
Changes with DirectoryCan change based on where you execute the Node.js script.Always refers to the location of the script file, remains constant.
Use CaseUseful for determining where the process is running (e.g., loading configuration files).Useful for resolving relative paths within a specific module.
ScopeA global value tied to the Node.js process.Tied to the script/module, cannot be changed.

Conclusion

Both process.cwd() and __dirname are essential tools in Node.js, but they serve different purposes:

  • process.cwd() is useful for determining the current working directory, which can change depending on where the Node.js process is started.
  • __dirname always refers to the directory where the current script is located, making it the go-to choice for resolving relative paths within your modules.

Next Article
Difference between __dirname and ./ in Node.js

S

saideepesh000
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Node.js-process-module

Similar Reads

  • Difference between __dirname and ./ in Node.js
    Working with any technology requires to interact with files and directories. Files and directories maintain a tree structure for easy access. Working with Node.js also requires accessing files using the file path which can be obtained using different commands. There are two ways of getting the curre
    3 min read
  • Difference between npm i and npm ci in Node.js
    npm i or npm install is used to install, modify, and update the dependencies in a project along with updating the dependencies in package-lock.json while npm ci only reinstalls all the packages mentioned in the package-lock.json with the specified versions and can't modify the lock packages. Let's d
    2 min read
  • Difference Between Development and Production in Node.js
    In this article, we will explore the key differences between development and production environments in Node.js. Understanding these differences is crucial for deploying and managing Node.js applications effectively. IntroductionNode.js applications can behave differently depending on whether they a
    3 min read
  • Difference Between Node.js and Python
    Node.js and Python are two of the most popular programming languages for backend development. Each has its own strengths and weaknesses, and the choice between them often depends on the specific requirements of the project. This article provides a detailed comparison of Node.js and Python, highlight
    4 min read
  • Difference between process.stdout.write and console.log in Node JS
    Both process.stdout.write and console.log in Node JS have basic functionality to display messages on the console. Basically console.log implement process.stdout.write while process.stdout.write is a buffer/stream that will directly output in our console. Difference between process.stdout.write and c
    2 min read
  • Difference between npm install and npm update in Node.js
    NPM is like a powerhouse for Node.js that contains all the necessary modules for the smooth running of the node.js application. It gets installed on our machine when we install Node.js on our Windows, Linux or MAC OS. How to install Node on the machine? Refer to this article. NPM has 580096 register
    5 min read
  • Differentiate between worker threads and clusters in Node JS.
    In this article, we will learn about worker threads & clusters, along with discussing the significant distinction that differentiates between worker threads & clusters. Table of Content What is Worker Thread in Node.JS?What is clusters in Node.JS?Difference between Worker threads and Cluster
    4 min read
  • Difference between spawn() and fork() methods in Node.js
    Node.js provides several ways to create child processes, enabling you to run tasks in parallel and leverage multi-core systems efficiently. Two commonly used methods for this purpose are spawn() and fork(). While they might seem similar, they serve different purposes and have distinct features. This
    4 min read
  • Difference between readFile and createReadStream in Node.js
    Node.js offers various methods for file handling, two of the most commonly used being readFile and createReadStream. fs.readFile and fs.createReadStream are both used to read files in Node.js, but they cater to different needs. fs.readFile reads the entire file into memory, making it suitable for sm
    5 min read
  • What is the difference between StrongNode and Node.js ?
    StrongLoop Node is a packaged distribution of Node.js, NPM, and the slc. The slc is a command-line utility and a set of supported npm modules that comes with StrongLoop Node for building and managing applications. Some tools and modules that come with the StrongLoop Node are Express, Connect, Passpo
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences