Discover how to stop your Node.js scripts from breaking just because you ran them from the wrong folder!
Why process.cwd and __dirname in Node.js? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you write a Node.js script that reads files using relative paths. You run it from different folders, and suddenly your script can't find the files anymore.
Manually guessing or hardcoding file paths is confusing and breaks easily when you move or run your code from different places. It's like trying to find your keys in a dark room without a flashlight.
Using process.cwd() and __dirname gives you reliable ways to know exactly where your code is running and where your files are, so you can build paths that always work.
const data = require('./data.json'); // breaks if run from another folder
const path = require('path'); const data = require(path.join(__dirname, 'data.json')); // always works
You can write Node.js scripts that find and load files correctly no matter where you run them from.
When building a tool that reads configuration files or templates, using __dirname ensures your tool always finds those files even if users run it from different folders.
process.cwd() tells you the current working folder where the script was started.
__dirname tells you the folder where the current script file lives.
Using them helps avoid broken file paths and makes your code more reliable.
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
