0
0
Node.jsframework~10 mins

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

Choose your learning style9 modes available
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.