How to get current directory in Node.js?
In Node.js, do you need to find the current directory? There are two built-in methods for doing this in Node.js: __dirname
and process.cwd()
. In this article, we’ll examine how to use both. You won’t have to install any NPM packages since both of these methods are built into Node.js.
It is time to get started
What is Node Js?
The Node.js runtime environment and library is an open-source, cross-platform software that lets web-based applications run outside the client’s browser. The language is primarily used for server-side programming and has been designed with the objective of achieving a non-blocking, event-driven architecture, which makes it perfect for traditional websites and API services on the back end, not to mention primary architectures that are based on real-time, push-based architectures. Every browser uses V8 JavaScript, which is the engine that powers Node.js. Isn’t that a bit confusing?
It simply means that you can run entire sites on a unified stack, making development and maintenance easy, and enabling you to meet the business objectives of every project. As open-source software, Node.js is constantly being modified and improved by developers around the world.
This is an especially crucial point to understand about Node.js because it is a runtime environment and neither a framework nor a library. Runtime environments (RTEs) contain Web APIs that developers can use to build code, as well as JavaScript engines to parse it. As a result, it is lightweight, flexible, and easy to deploy, all features that will accelerate and optimize your app development process.
NodeJs: Why Do We Use It?
NodeJs is a popular server-side language for creating web applications, and we use it for a variety of reasons. Here are some of them:
- The NodeJs engine is based on the V8 engine in Google Chrome, therefore, it runs quickly and has a fast execution time.
- Node Package Manager contains more than 50,000 bundles, so developers can import any one of them according to their needs, thereby saving time.
- The NodeJS framework does not need any API calls to return data, so it is very useful when building data-intensive and real-time web applications. Due to its asynchronous nature, it is completely non-blocking.
- Because NodeJs synchronizes the code between the client and server, the loading time for an audio or video is reduced.
- Because NodeJs is open-source and nothing more than a JavaScript framework, beginning a project with NodeJs is a very simple task for developers already familiar with JavaScript.
What are directories?
The directories on a computer are locations for storing files. This is a structure for cataloging files and directories in a file system. Hierarchical structures, such as folders and files, are set up in ways that resemble tree structures. As an example, a subdirectory is a directory that resides inside another directory. Generally, directories and subdirectories are referred to as parents and children, respectively. Root directories are the topmost files in a file system. A directory is like a filing cabinet with folders inside. Files are contained within these folders.
File systems organize files in folders within storage devices such as hard disks or solid-state drives. Depending on your operating system, user files and system files exist in different directories. Unix, Linux, MS-DOS, and OS/2 are hierarchical file systems with directories. Directories are known as folders in GUIs such as Microsoft Windows.
Information can be easily accessed by storing specific types of files in folders. It is also possible to store programs in a directory. The files associated with an installed program are located in a directory containing subdirectories that contain additional files. A directory reduces the possibility of files getting overwritten, modified, or deleted by other programs.
Different methods to find the current directory in Node.js
Method 1: Use __dirname to get the current directory
The __dirname
method is the first method to get the current directory path. Using this Node.js core module, you can find out what directory the JavaScript file or module is loaded into.
As an example, here’s how you might use it:
console.log("Current directory:", __dirname);
Code language: JavaScript (javascript)
It prints: Current Directory: /Users/muthu/nodejsprojects
The path.dirname()
method combined with the __filename
module can also produce the same results.
path.dirname()
method combined with the __filename
module can also produce the same results.
A path can be passed to the path.dirname()
function to get the working directory of the path. A full path to the current file is provided in the
A path can be passed to the path.dirname()
function to get the working directory of the path. A full path to the current file is provided in the _filename
module.
Combining those two methods will give you the current directory’s path:
path.dirname(__filename)
Code language: JavaScript (javascript)
It Prints: Current Directory:/Users/muthu/nodejsprojects
Method 2: Use process.cwd to get the current directory ()
process.cwd()
works similarly to the _dirname
variable. Using the root directory as a reference, it prints the path to the current working directory.
console.log("Current directory:", process.cwd());
Code language: JavaScript (javascript)
It prints: Current Directory: /Users/muthu/nodejsprojects
Conclusion
We’ve learned two methods for getting the current directory path.dirname()
and process.cwd()
.
I hope you enjoyed reading this and happy coding!
Sharing is caring
Did you like what muthuannamalai wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: