How to Get the Path to the Current Script with Node.js

From understanding what Node.js and the current script are, to knowing how to get a path to the current script with Node.js, this article will help you.

Scripts are essential tools for developers and system administrators in order to streamline processes and are usually organized into directories just like files within a file system. This makes getting the path to the scripts an essential task in the development process. This article demonstrates how we can use the JavaScript runtime environment, Node.js to get the path to the current script.

Also Read: Asynchronous Programming in Node.js

Overview of Node.js

Node.js is a versatile, open-source server environment that is compatible with multiple operating systems, including Windows, Linux, Unix, and macOS. It serves as a back-end JavaScript runtime environment, utilizing the V8 JavaScript engine to execute JavaScript code beyond the confines of web browsers.

Developers can leverage Node.js to create command-line tools and implement server-side scripting. This capability to run JavaScript on the server is often harnessed to dynamically generate web page content prior to delivering it to users’ web browsers. Consequently, Node.js promotes a “JavaScript everywhere” approach, bringing together web application development under a single programming language, eliminating the need for distinct languages on the server and client sides.

Read More: What is Node.js?

What is Current Script?

Scripts on a computer are like sets of instructions. They serve as a structured series of commands or actions that dictate how a particular task should be carried out. Scripting, in essence, is the process of creating these sets of instructions to automate various processes and achieve specific goals. Scripts can range from simple, one-time-use commands to complex, reusable programs that facilitate tasks and workflows.

Scripting languages, like Python, JavaScript, and Bash, provide the tools to create and execute scripts. These scripts can manipulate data, interact with software applications, perform system operations, and more.

A script that is currently being executed or in use is usually referred to as the current script. For example, if you are writing JavaScript code, the script you are writing in is considered the current script.

With this small introduction, let’s see the process of getting its path in Node.js.

Getting the Path to the Current Script in Node.js

There are two ways to get the path to the current script in Node.js. The first one is using __dirname while the other is using process.cwd(). We have discussed both of these methods below.

Using __dirname

__dirname is a Node.js global variable that returns the directory name of the current module. It represents the absolute path to the directory containing the current JavaScript file. It always refers to the directory in which the script is found. This means it’s a dependable way to refer to files relative to the script’s location. The path returned by __dirname is typically used for loading modules or accessing files in the current script’s directory.

console.log(__dirname);

Using the above code, we can find out the absolute path of the current script’s directory.

Sample Input/Output:

Using __dirname
Output of __dirname


Get the path with the file name:

The path.dirname() method, when used in conjunction with the __filename module, can give you an outcome with the filename as well along with the name of the directory.

Additionally, we can provide a path to the path.dirname() function to obtain the directory where the path is located. The __filename module supplies the complete path to the current file.

path.dirname(__filename)

Sample Input/Output:

Using path.dirname()
Output of path.dirname()

Using process.cwd()

process.cwd() is a Node.js method for obtaining the Node.js process’s current working directory (CWD). It returns the directory where the Node.js process was started. It can change if the Node.js process is launched from a different directory rather __dirname, which always points to the script’s directory.

It reflects the directory in which the Node.js process is running, which may or may not be the same as the directory in which the script is located. It is primarily used to determine the Node.js process’s current working directory, which is useful when accessing files or resources relative to the process’s current directory.

console.log(process.cwd());

Using the above code, we get a similar outcome as the one with the above method i.e. using __dirname.

Sample Input/Output:

Using process.cwd()
Output of process.cwd()

Summary

Scripts are tools used by developers to streamline the processes and workflows. Thus, getting the path to the current script is an essential step in the development process. In order to get the path to the current script we can use two methods that are either using __dirname or process.cwd(). Both of these methods provide the path to the directory where the current script is located. We can use path.dirname() in conjunction with the __filename module to get the complete path along with the path to the file containing the current script.

Reference

https://stackoverflow.com/questions/3133243/how-do-i-get-the-path-to-the-current-script-with-node-js

Khushi Soni
Khushi Soni
Articles: 3