askvity

How do you get the absolute path in Node?

Published in Node.js Paths 3 mins read

In Node.js, the most common way to get the absolute path is by using the built-in __filename and __dirname global variables.

Understanding __filename and __dirname

Node.js provides two helpful global variables that give you immediate access to absolute path information within any script file. These variables are available globally in Node.js modules.

  • __filename: This variable holds the absolute path to the currently executing script file.
  • __dirname: This variable holds the absolute path to the directory containing the currently executing script file.

As stated in the reference, you can use the __filename variable to get the absolute path to the current script file, and __dirname to get the absolute path to the directory containing the current script.

Why Use Absolute Paths?

Using absolute paths is crucial for making your Node.js applications reliable and portable.

  • Consistency: Relative paths can break if the current working directory changes, which can happen easily depending on how a script is executed. Absolute paths always point to the same location regardless of where the script is run from.
  • Reliability: When requiring modules, reading files, or writing files, using absolute paths ensures Node.js knows exactly where to look or save.

Practical Usage and Examples

Here are some common ways to use __filename and __dirname.

Getting the Script File Path

To get the full path including the filename:

console.log(__filename);
// Example Output: /Users/username/projects/my-node-app/src/index.js

Getting the Directory Path

To get the path to the folder containing the script:

console.log(__dirname);
// Example Output: /Users/username/projects/my-node-app/src

Reading a File Relative to the Script

You can combine __dirname with the path module to reliably construct absolute paths to other files within the same directory or subdirectories.

const path = require('path');
const fs = require('fs');

// Path to a file named 'data.txt' in the same directory as the script
const filePath = path.join(__dirname, 'data.txt');

fs.readFile(filePath, 'utf8', (err, data) => {
  if (err) {
    console.error("Error reading file:", err);
    return;
  }
  console.log("File content:", data);
});

Requiring Modules Relative to the Script

Similarly, you can reliably require modules located relative to the current file:

const myModule = require(path.join(__dirname, '..', 'lib', 'myModule'));
// This would require a module located at /Users/username/projects/my-node-app/lib/myModule.js

__filename vs __dirname

Here's a quick comparison:

Variable Description Example (for /app/src/script.js)
__filename Absolute path to the current script file. /app/src/script.js
__dirname Absolute path to the directory of the script file. /app/src

These built-in variables are the most straightforward way to get absolute path information directly within your Node.js script files.

Related Articles