Difference between __dirname and process.cwd() in Node.js
When working with file paths in Node.js, you will frequently
encounter __dirname and process.cwd(). While
both of these utilities return absolute directory paths, they point to
different locations depending on how and where your application is
executed. This article explains the fundamental differences between the
two, illustrates their behavior with practical examples, and outlines
when to use each.
What is __dirname?
__dirname is an environment variable (specifically, a
local variable available in every CommonJS module) that returns the
absolute path of the directory containing the currently executing
JavaScript file.
- Behavior: It is tied directly to the file’s location on the filesystem.
- Consistency: The value of
__dirnameremains constant for a specific file, regardless of where you run thenodecommand in your terminal.
What is process.cwd()?
process.cwd() is a built-in method of the global Node.js
process object. It returns the absolute path of the Current
Working Directory (CWD) of the Node.js process—in other words, the
directory from which you ran the node terminal command.
- Behavior: It is tied to the terminal’s active directory at the moment of execution.
- Consistency: The value of
process.cwd()is dynamic and changes based on where the user executes the script.
Illustrative Example
To understand the practical difference, consider the following directory structure:
/my-project
├── app.js
└── src/
└── helper.js
Inside src/helper.js, we write the following code:
console.log('__dirname: ', __dirname);
console.log('process.cwd():', process.cwd());Depending on where you run the Node.js process, the output will change:
Case
1: Running the script from the project root
(/my-project)
If your terminal is in /my-project and you run the
script using its relative path:
node src/helper.jsOutput: *
__dirname: /my-project/src (Points to the location of
helper.js) * process.cwd(): /my-project
(Points to where you ran the command)
Case
2: Running the script from the src directory
(/my-project/src)
If you navigate into the src directory and run the
script directly:
cd src
node helper.jsOutput: *
__dirname: /my-project/src (Remains unchanged) *
process.cwd(): /my-project/src (Changes to reflect your
current terminal folder)
Comparison Summary
| Feature | __dirname |
process.cwd() |
|---|---|---|
| Definition | Directory of the current script file | Current working directory of the process |
| Scope | Module-level variable (CommonJS) | Global process method |
| Dependability | Stable (relative to the file location) | Dynamic (relative to terminal execution location) |
| ES Modules Compatibility | Not available by default in ES modules (requires workaround) | Fully supported in both CommonJS and ES modules |
When to Use Which
- Use
__dirnamewhen you need to reference assets, configuration files, or other source code files that are located relative to the current codebase. For example, reading a local JSON config file inside a module or loading a relative template. - Use
process.cwd()when you are building command-line interfaces (CLIs) or tools that need to interact with the user’s active workspace. For example, if your script creates, reads, or updates files in the folder where the user currently is,process.cwd()ensures you access the user’s active directory.