Last active
April 23, 2020 11:35
-
-
Save naorzr/c6e26e7722d6b48996c7f3b2c576a2ab to your computer and use it in GitHub Desktop.
Get application root in nodejs for windows and unix system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Node version 12.0 | |
// Typescript version 3.8 | |
import fs from "fs"; | |
import path from "path"; | |
const getAppRoot = (curPath: string, rootIndicator = 'package.json'): string | Error=> { | |
if (curPath.length === 0 || curPath === path.sep) { | |
throw new Error(`Couldn't find root dir`) | |
} else if (fs.existsSync(path.join(curPath, rootIndicator))) { | |
return curPath | |
} else { | |
return getAppRoot(path.dirname(curPath)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment