Bird
Raised Fist0
Node.jsframework~20 mins

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

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
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.

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