Bird
Raised Fist0
Node.jsframework~10 mins

process.cwd and __dirname in Node.js - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - process.cwd and __dirname
Start Node.js script
Read __dirname
Returns script folder path
Call process.cwd()
Returns current working directory
Use paths for file operations
Script ends
The script starts, reads __dirname for script folder path, calls process.cwd() for current working directory, then uses these paths for file tasks.
Execution Sample
Node.js
console.log('__dirname:', __dirname);
console.log('process.cwd():', process.cwd());
Prints the folder path of the script and the current working directory where Node.js was started.
Execution Table
StepActionValue of __dirnameValue of process.cwd()Output
1Start script in /home/user/project/scripts/home/user/project/scripts/home/user/project/scriptsNo output yet
2Read __dirname/home/user/project/scripts/home/user/project/scripts__dirname: /home/user/project/scripts
3Call process.cwd()/home/user/project/scripts/home/user/project/scriptsprocess.cwd(): /home/user/project/scripts
4Print both values/home/user/project/scripts/home/user/project/scriptsPrinted both paths
5Script ends/home/user/project/scripts/home/user/project/scriptsExecution complete
💡 Script ends after printing __dirname and process.cwd() values
Variable Tracker
VariableStartAfter Step 2After Step 3Final
__dirnameundefined/home/user/project/scripts/home/user/project/scripts/home/user/project/scripts
process.cwd()undefinedundefined/home/user/project/scripts/home/user/project/scripts
Key Moments - 2 Insights
Why are __dirname and process.cwd() different?
Because __dirname is the folder where the script file lives, while process.cwd() is the folder where you started Node.js. See execution_table steps 2 and 3.
Can process.cwd() change during script execution?
Yes, if you use process.chdir(), process.cwd() changes but __dirname stays the same. This is shown by how __dirname is fixed in step 2 but process.cwd() can vary.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of __dirname at step 3?
A/home/user/project/scripts
B/home/user/project
Cundefined
D/home/user
💡 Hint
Check the __dirname column at step 3 in execution_table
At which step does process.cwd() get its value?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at process.cwd() column in execution_table to see when it changes from undefined
If you start Node.js in /home/user/project/scripts, what will process.cwd() show?
A/home/user/project
B/home/user/project/scripts
C/home/user
Dundefined
💡 Hint
process.cwd() shows the folder where Node.js was started, see variable_tracker and key_moments
Concept Snapshot
__dirname gives the folder path of the current script file.
process.cwd() gives the current working directory where Node.js was started.
They can be different if you run the script from another folder.
Use __dirname for script-relative paths.
Use process.cwd() for user working directory paths.
process.cwd() can change during runtime with process.chdir().
Full Transcript
When you run a Node.js script, __dirname holds the folder path where the script file is located. This never changes during the script. process.cwd() returns the current working directory where you started Node.js. This can be different from __dirname if you run the script from another folder. For example, if your script is in /home/user/project/scripts but you start Node.js from /home/user/project, __dirname will be /home/user/project/scripts and process.cwd() will be /home/user/project. You can use __dirname to access files relative to your script, and process.cwd() to access files relative to where the user started the program. process.cwd() can also change if you call process.chdir() during the script. This trace shows step-by-step how these values are read and printed.

Practice

(1/5)
1. What does process.cwd() return in a Node.js program?
easy
A. The folder where the current script file is located
B. The folder where you started the Node.js program
C. The full path of the current script file
D. The user's home directory

Solution

  1. Step 1: Understand process.cwd() purpose

    process.cwd() returns the current working directory where the Node.js process was started, not the script location.
  2. Step 2: Compare with __dirname

    __dirname gives the script's folder, which is different from the working directory if you run the script from another folder.
  3. Final Answer:

    The folder where you started the Node.js program -> Option B
  4. Quick Check:

    process.cwd() = start folder [OK]
Hint: Remember: cwd = where you run node from [OK]
Common Mistakes:
  • Confusing process.cwd() with __dirname
  • Thinking it returns the script file path
  • Assuming it returns the user's home directory
2. Which of the following is the correct way to get the directory name of the current script file in Node.js?
easy
A. __dirname
B. process.dirName
C. process.cwd()
D. currentDir()

Solution

  1. Step 1: Identify the built-in variable for script folder

    __dirname is a Node.js global variable that holds the directory path of the current script file.
  2. Step 2: Check other options for validity

    process.cwd() returns the working directory, not script folder. The others are not valid Node.js properties or functions.
  3. Final Answer:

    __dirname -> Option A
  4. Quick Check:

    __dirname = script folder [OK]
Hint: Use __dirname for script folder path [OK]
Common Mistakes:
  • Using process.cwd() instead of __dirname
  • Trying to use non-existent properties like process.dirName
  • Confusing function names
3. Consider this code run from folder /home/user/projects with script located at /home/user/projects/app/server.js:
console.log(process.cwd());
console.log(__dirname);
What will be the output?
medium
A. /home/user/projects /home/user/projects/app
B. /home/user/projects/app /home/user/projects/app
C. /home/user/projects/app /home/user/projects
D. /home/user/projects /home/user/projects

Solution

  1. Step 1: Understand process.cwd() output

    Since the program is started in /home/user/projects, process.cwd() returns this folder.
  2. Step 2: Understand __dirname output

    The script is located in /home/user/projects/app/server.js, so __dirname returns /home/user/projects/app.
  3. Final Answer:

    /home/user/projects /home/user/projects/app -> Option A
  4. Quick Check:

    cwd = start folder, __dirname = script folder [OK]
Hint: cwd = run folder, __dirname = script folder [OK]
Common Mistakes:
  • Swapping outputs of process.cwd() and __dirname
  • Assuming both return the same path
  • Ignoring where the script file is located
4. You wrote this code in /app/index.js and ran it from /app folder:
console.log(process.dirName);
What will happen when you run this script?
medium
A. It prints the current working directory
B. It prints the script's directory
C. It throws a ReferenceError
D. It prints undefined

Solution

  1. Step 1: Check the property used

    process.dirName is not a valid property in Node.js. The correct property is __dirname.
  2. Step 2: Understand the error caused

    Accessing an undefined property on process does not throw an error, but since process is an object, process.dirName is undefined. However, trying to log undefined prints 'undefined' without error.
  3. Step 3: Confirm behavior

    Since process.dirName is undefined, console.log prints 'undefined'. No ReferenceError occurs.
  4. Final Answer:

    It prints undefined -> Option D
  5. Quick Check:

    Invalid property logs undefined [OK]
Hint: Check exact property names; typos cause undefined [OK]
Common Mistakes:
  • Expecting ReferenceError for undefined property
  • Confusing __dirname with process.dirName
  • Assuming process.dirName exists
5. You want to read a file named config.json located in the same folder as your script /project/src/app.js. You run the script from /project folder. Which code snippet correctly builds the path to config.json to read it safely regardless of where you run the script?
hard
A. const configPath = './config.json';
B. const path = require('path'); const configPath = path.join(process.cwd(), 'config.json');
C. const path = require('path'); const configPath = path.join(__dirname, 'config.json');
D. const configPath = process.cwd() + '/src/config.json';

Solution

  1. Step 1: Identify script and file locations

    The script is at /project/src/app.js and config.json is in the same folder /project/src.
  2. Step 2: Understand path building with __dirname

    Using __dirname gives the script folder regardless of where you run the script, so joining __dirname with config.json correctly points to the file.
  3. Step 3: Analyze other options

    const path = require('path'); const configPath = path.join(process.cwd(), 'config.json'); uses process.cwd() which is /project, so it looks for /project/config.json (wrong folder). const configPath = './config.json'; is relative and depends on run folder, risky. const configPath = process.cwd() + '/src/config.json'; hardcodes path and may break on different OS or run folders.
  4. Final Answer:

    const path = require('path'); const configPath = path.join(__dirname, 'config.json'); -> Option C
  5. Quick Check:

    Use __dirname to locate files relative to script [OK]
Hint: Use __dirname + path.join for script-relative files [OK]
Common Mistakes:
  • Using process.cwd() which depends on run folder
  • Using relative paths without __dirname
  • Hardcoding paths without path.join