Identify the root of the file system using Node.js

I perform the basic operation when I start from a given directory and I go to the file system until I am at the root. On Linux / Mac, the root is obviously, /and on Windows it could be a C:\different drive name, of course. My question is whether there is a way for Node.js to determine what the root directory of the file system is.

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

+5
source share
2 answers

Wouldn't that work?

var path = require("path");
var os = require("os");
var root = (os.platform == "win32") ? process.cwd().split(path.sep)[0] : "/"
+2
source

There is nothing special about running Node.js, the answer is a simple regular expression:

/^([^\\/]*[\\/]).*/.test(process.cwd())
var root = RegExp.$1;

This should get the root of CWD for Windows and Linux.

+1
source

All Articles