Skip to content

Instantly share code, notes, and snippets.

@naorzr
Last active April 23, 2020 11:35
Show Gist options
  • Save naorzr/c6e26e7722d6b48996c7f3b2c576a2ab to your computer and use it in GitHub Desktop.
Save naorzr/c6e26e7722d6b48996c7f3b2c576a2ab to your computer and use it in GitHub Desktop.
Get application root in nodejs for windows and unix system
// 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