These help you find out where your program is running from. This is useful when you want to work with files or folders in your project.
process.cwd and __dirname in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
process.cwd() __dirname
process.cwd() returns the current working directory where you started the Node.js process.
__dirname is a variable that holds the directory name of the current script file.
console.log(process.cwd())
console.log(__dirname)
const path = require('path'); console.log(path.join(__dirname, 'data', 'file.txt'))
This program shows the difference between process.cwd() and __dirname. It also builds a path to a file named example.txt located in the same folder as the script.
const path = require('path'); console.log('Current working directory:', process.cwd()); console.log('Current script directory:', __dirname); const filePath = path.join(__dirname, 'example.txt'); console.log('Path to example.txt:', filePath);
process.cwd() can change if you use process.chdir() to change folders during runtime.
__dirname is fixed to the script file location and does not change.
Use path.join() to safely build file paths that work on all operating systems.
process.cwd() tells you where you started your Node.js program.
__dirname tells you where the current script file lives.
Both help you work with files and folders in your project safely and clearly.
Practice
process.cwd() return in a Node.js program?Solution
Step 1: Understand
process.cwd()purposeprocess.cwd()returns the current working directory where the Node.js process was started, not the script location.Step 2: Compare with
__dirname__dirnamegives the script's folder, which is different from the working directory if you run the script from another folder.Final Answer:
The folder where you started the Node.js program -> Option BQuick Check:
process.cwd()= start folder [OK]
- Confusing
process.cwd()with__dirname - Thinking it returns the script file path
- Assuming it returns the user's home directory
Solution
Step 1: Identify the built-in variable for script folder
__dirnameis a Node.js global variable that holds the directory path of the current script file.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.Final Answer:
__dirname -> Option AQuick Check:
__dirname= script folder [OK]
- Using process.cwd() instead of __dirname
- Trying to use non-existent properties like process.dirName
- Confusing function names
/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?
Solution
Step 1: Understand
Since the program is started inprocess.cwd()output/home/user/projects,process.cwd()returns this folder.Step 2: Understand
The script is located in__dirnameoutput/home/user/projects/app/server.js, so__dirnamereturns/home/user/projects/app.Final Answer:
/home/user/projects /home/user/projects/app -> Option AQuick Check:
cwd = start folder, __dirname = script folder [OK]
- Swapping outputs of process.cwd() and __dirname
- Assuming both return the same path
- Ignoring where the script file is located
/app/index.js and ran it from /app folder:
console.log(process.dirName);What will happen when you run this script?
Solution
Step 1: Check the property used
process.dirNameis not a valid property in Node.js. The correct property is__dirname.Step 2: Understand the error caused
Accessing an undefined property onprocessdoes not throw an error, but sinceprocessis an object,process.dirNameis undefined. However, trying to log undefined prints 'undefined' without error.Step 3: Confirm behavior
Sinceprocess.dirNameis undefined,console.logprints 'undefined'. No ReferenceError occurs.Final Answer:
It prints undefined -> Option DQuick Check:
Invalid property logs undefined [OK]
- Expecting ReferenceError for undefined property
- Confusing __dirname with process.dirName
- Assuming process.dirName exists
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?Solution
Step 1: Identify script and file locations
The script is at/project/src/app.jsandconfig.jsonis in the same folder/project/src.Step 2: Understand path building with
Using__dirname__dirnamegives the script folder regardless of where you run the script, so joining__dirnamewithconfig.jsoncorrectly points to the file.Step 3: Analyze other options
const path = require('path'); const configPath = path.join(process.cwd(), 'config.json'); usesprocess.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.Final Answer:
const path = require('path'); const configPath = path.join(__dirname, 'config.json'); -> Option CQuick Check:
Use __dirname to locate files relative to script [OK]
- Using process.cwd() which depends on run folder
- Using relative paths without __dirname
- Hardcoding paths without path.join
