Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying the File System Root with Node.js

I'm doing a basic operation where I start from a given directory, and I traverse up the filesystem until I hit the root. On Linux/Mac, the root is obviously / and on Windows it can be C:\ or another drive letter of course. My question is whether or not there is a way for Node.js to identify what the root directory of the filesystem is.

Currently, I'm resorting to simply checking the last directory against path.normalize(dir + "/../") to see if it stops changing. Is there a process property/method out there? Maybe a module?

like image 610
Dominic Barnes Avatar asked Mar 11 '12 02:03

Dominic Barnes


People also ask

How do I find the path of a file in node?

To check the path in asynchronous mode in the fs module we can use fs. stat() method. The fs. stat() method takes two parameters, first parameter is the path and the second is the callback function with two parameters, one is for error in case error occurred and the second parameter is the data retrieved by calling fs.

What is __ Dirname in node?

It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.


1 Answers

Another one, using path.parse.

const path = require('path')

const getRootDir = () => path.parse(process.cwd()).root
like image 62
Shou Avatar answered Sep 28 '22 21:09

Shou