Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Identifying the File System Root with Node.js

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?

Problem courtesy of: Dominic Barnes


Would this not work?

var path = require("path");
var os = require("os");
var root = (os.platform == "win32") ? process.cwd().split(path.sep)[0] : "/"
Solution courtesy of: Joshua F. Rountree


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Identifying the File System Root with Node.js


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription