0
0
Node.jsframework~20 mins

process.cwd and __dirname in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding process.cwd() vs __dirname
What does process.cwd() return compared to __dirname in a Node.js script?
A<code>process.cwd()</code> and <code>__dirname</code> both return the same directory path always.
B<code>process.cwd()</code> returns the directory of the current script file; <code>__dirname</code> returns the directory where the Node.js process was started.
C<code>process.cwd()</code> returns the directory where the Node.js process was started; <code>__dirname</code> returns the directory of the current script file.
D<code>process.cwd()</code> returns the root directory of the system; <code>__dirname</code> returns the home directory of the user.
Attempts:
2 left
💡 Hint
Think about where you run the command versus where the script file is located.
component_behavior
intermediate
1:30remaining
Output of process.cwd() and __dirname in nested folders
Given this folder structure:
project/
  app/
    script.js

If you run node app/script.js from the project folder, what will console.log(process.cwd()) and console.log(__dirname) output inside script.js?
Node.js
console.log(process.cwd());
console.log(__dirname);
Aprocess.cwd() outputs '/project/app'; __dirname outputs '/project'
Bprocess.cwd() outputs '/project/app'; __dirname outputs '/project/app'
Cprocess.cwd() outputs '/project'; __dirname outputs '/project'
Dprocess.cwd() outputs '/project'; __dirname outputs '/project/app'
Attempts:
2 left
💡 Hint
Remember where you run the command and where the script file is.
📝 Syntax
advanced
1:30remaining
Identify the error in using __dirname in ES modules
Which option correctly explains the behavior of __dirname in a Node.js ES module (using type: 'module' in package.json)?
A<code>__dirname</code> is not defined in ES modules and will cause a ReferenceError if used directly.
B<code>__dirname</code> works the same in ES modules as in CommonJS modules.
C<code>__dirname</code> is automatically replaced with the current working directory in ES modules.
D<code>__dirname</code> is replaced with <code>process.cwd()</code> in ES modules.
Attempts:
2 left
💡 Hint
Think about the difference between CommonJS and ES modules in Node.js.
🔧 Debug
advanced
2:00remaining
Debugging path issues with process.cwd() and __dirname
A developer writes this code in a script located at /home/user/project/utils/helper.js and runs it from /home/user folder:
const path = require('path');
console.log(path.join(process.cwd(), 'utils', 'helper.js'));
console.log(path.join(__dirname, 'helper.js'));

What will be the output and what problem might occur?
AFirst line outputs '/home/user/utils/helper.js', second line outputs '/home/user/project/utils/helper.js'; the first path is incorrect if the script expects the helper.js file relative to the script location.
BFirst line outputs '/home/user/project/utils/helper.js', second line outputs '/home/user/utils/helper.js'; the second path is incorrect.
CBoth lines output the same correct path '/home/user/project/utils/helper.js'.
DBoth lines cause a runtime error because __dirname is undefined.
Attempts:
2 left
💡 Hint
Check what process.cwd() returns when running from a different folder.
state_output
expert
2:00remaining
Predict output when changing working directory inside script
Consider this Node.js script changeDir.js located at /app/scripts/changeDir.js.
console.log('Before:', process.cwd());
process.chdir('/');
console.log('After:', process.cwd());
console.log('__dirname:', __dirname);

If you run node scripts/changeDir.js from /app, what will be the output?
A
Before: /app/scripts
After: /
__dirname: /app/scripts
B
Before: /app
After: /
__dirname: /app/scripts
C
Before: /app
After: /app
__dirname: /app/scripts
D
Before: /app
After: /
__dirname: /
Attempts:
2 left
💡 Hint
Remember what process.chdir() does and that __dirname does not change.