node get root directory

To get the root directory in Node.js, you can follow these steps:

  1. Import the path module: In Node.js, the path module provides utilities for working with file paths. To use its functions, you need to import the module using the require statement:
const path = require('path');
  1. Use the path.parse() method: The path.parse() method takes a file path as input and returns an object containing various properties of the path. One of these properties is root, which represents the root directory of the file system:
const rootDirectory = path.parse('/path/to/file').root;
  1. Print the root directory: You can simply log the rootDirectory variable to the console to display the root directory:
console.log(rootDirectory);

By following these steps, you can obtain the root directory in Node.js using the path module.