Get the Path of the Current Working Directory in NodeJS
Do you need to get the path of the current working directory in Node.js?
Node.js has two built-in ways to do this: __dirname and the process.cwd() function.
In this article, we'll cover how to use both! And since both of those methods are built into Node.js, you won't need to install any NPM packages.
Let's get started!
Table of Contents
Method 1 — __dirname
The first method to get the path of the current working directory is the __dirname method. This is a Node.js core module that gets the current path of whatever directory the current Node.js process is running in.
Here's how you use it out in the wild:
// Prints: /Users/Billy_Bob/projects
And you can also get the same result by using the path.dirname() method combined with the __filename module.
The path.dirname() function takes a path and returns the working directory of the given path. And the __filename module is the full path of the current file.
You can combine those two methods to get the path of the current directory like this:
// Prints: /Users/Billy_Bob/projects
Pretty easy, right? We'll show you one additional way to do this in the next section.
Method 2 — process.cwd()
The process.cwd() function returns the current working directory of the Node.js process.
Here's what the code looks like:
// Prints: /Users/Billy_Bob/projects
This is a simple option and built directly into Node.js, so no NPM packages are required.
In this article, you learned two different methods to get the current file path of the working directory: __dirname and process.cwd().
Thanks for reading and happy coding!
Have any questions or comments about this article? You can contact me via email at or on Twitter at @CoderRocketFuel.
If you enjoyed this free content, please consider supporting what I do by Buying Me a Coffee.