App Root Dir

raw JSON →
1.0.2 verified Sun Apr 19 auth: no javascript abandoned

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.

error ReferenceError: require is not defined
cause Attempting to use `require('app-root-dir')` in an ES module context.
fix
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.
error TypeError: appRootDir.get is not a function
cause The module was not correctly imported or resolved, leading to `appRootDir` being undefined or not the expected module object.
fix
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.
error Returned root directory is incorrect or unexpected (e.g., '/usr/local/bin' or user home directory)
cause The package's heuristic for inferring the root directory failed in the current execution environment (e.g., global CLI, bundled app, specific process manager, or complex monorepo structure).
fix
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.
deprecated The `app-root-dir` package is no longer maintained, with its last update occurring in 2015. It lacks support for modern Node.js features like ESM and may have compatibility issues with newer environments or tooling.
fix Consider migrating to actively maintained alternatives like `app-root-path` which offer better compatibility and features.
gotcha The `set()` method modifies a global variable, meaning subsequent calls to `get()` from anywhere in your application, even across different module instances, will return the last manually set value. This global state can lead to unexpected behavior and makes testing challenging.
fix Avoid using `set()` unless absolutely necessary and understand its global implications. For most cases, rely on the package's inference or pass paths explicitly.
gotcha The package relies on heuristics (checking `process.cwd()` or `node_modules` path) to determine the root directory. This can be unreliable in complex project setups like monorepos, bundled applications, or when running scripts from subdirectories, potentially returning an incorrect root path.
fix 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.
breaking This module is CommonJS-only and does not provide an ESM entry point. Attempting to `import` it in an ES module context will result in a `ReferenceError` or similar module resolution error.
fix 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).
npm install app-root-dir
yarn add app-root-dir
pnpm add app-root-dir

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.

const appRootDir = require('app-root-dir');
const path = require('path');

// Get the inferred application's root directory
const currentRootDir = appRootDir.get();
console.log(`Current inferred root directory: ${currentRootDir}`);

// Manually set the application's root directory (this sets a global state)
// This should typically be done early in your application's lifecycle.
// For demonstration, we'll set it to a mock path.
const mockAppPath = path.resolve(process.cwd(), 'temp_app_root');
console.log(`Setting application root directory to: ${mockAppPath}`);
appRootDir.set(mockAppPath);

// Verify the change
const newRootDir = appRootDir.get();
console.log(`New inferred root directory after setting: ${newRootDir}`);

// Reset for further demonstration or testing (optional, for cleanup)
// In a real app, you might not reset it like this.
// Note: There is no direct 'unset' or 'clear' function.
// The only way to revert is to set it to another value.
// For simplicity in this example, we will not 'unset' it.