App Root Dir is a minimalist Node.js module designed to infer the root directory of the currently running application. Published as version 1.0.2 over a decade ago (last updated in 2015), this package is no longer actively maintained. It employs a heuristic based on the presence of `package.json` at `process.cwd()` or by traversing `node_modules` paths to identify the project root. Its primary functions are `get()` to retrieve the root directory and `set()` to manually configure it, notably using a global variable, which can lead to unexpected side effects. Due to its age, it exclusively supports CommonJS and lacks native ESM support or adaptations for modern development practices like monorepos or bundled applications, where its heuristic may prove unreliable. For new projects, actively maintained alternatives that provide more robust and configurable root path resolution, including ESM compatibility and TypeScript types, are strongly recommended.
npm install app-root-dirVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to retrieve the application's root directory and how to manually set it. It highlights the CommonJS `require` syntax and the global state impact of the `set()` method.
Consider migrating to actively maintained alternatives like `app-root-path` which offer better compatibility and features.
Avoid using `set()` unless absolutely necessary and understand its global implications. For most cases, rely on the package's inference or pass paths explicitly.
Always verify the returned path, especially in non-standard deployment environments. For complex setups, you might need to manually configure the root path or use a more sophisticated path resolution library.
Use `const appRootDir = require('app-root-dir');` in CommonJS modules. If your project is pure ESM, you must use a different, ESM-compatible package or adapt your module loading strategy (e.g., using `createRequire` in Node.js for legacy CJS modules).This package is CommonJS-only. If your project uses ES modules, you will need to find an alternative library that supports ESM, or use Node.js's `createRequire` for interoperability with legacy CJS modules.
Ensure `require('app-root-dir')` is correctly placed and that `app-root-dir` is installed. Verify no other variable is overwriting `appRootDir` before its use.Manually set the root directory early in your application's entry point using `require('app-root-dir').set(path.resolve(__dirname, '..'))` or a similar explicit path. Alternatively, ensure a `package.json` file is present in a discoverable parent directory for the heuristic to work as expected.No dependency data recorded yet.