App Root Dir
raw JSON →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.
Common errors
error ReferenceError: require is not defined ↓
createRequire for interoperability with legacy CJS modules. error TypeError: appRootDir.get is not a function ↓
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) ↓
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. Warnings
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. ↓
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. ↓
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. ↓
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. ↓
Install
npm install app-root-dir yarn add app-root-dir pnpm add app-root-dir Imports
- appRootDir wrong
import appRootDir from 'app-root-dir';correctconst appRootDir = require('app-root-dir'); - get wrong
import { get } from 'app-root-dir';correctconst rootPath = require('app-root-dir').get(); - set wrong
import { set } from 'app-root-dir';correctrequire('app-root-dir').set(__dirname);
Quickstart
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.