Registry / devops / app-root-dir

app-root-dir

JSON →
library1.0.2jsnpmunverified

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-dir
INSTALL
IMPORT
SIG · APP-ROOT-DIR
A
app-root-dir
devopsjavascriptv1.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

appRootDir
const appRootDir = require('app-root-dir');
import appRootDir from 'app-root-dir';
This package is CommonJS-only and does not support ESM imports.
get
const rootPath = require('app-root-dir').get();
import { get } from 'app-root-dir';
Access the `get` method directly from the CommonJS module export.
set
require('app-root-dir').set(__dirname);
import { set } from 'app-root-dir';
The `set` method modifies a global variable, affecting all instances of the module, which can be a source of unexpected behavior.

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.
Debug
Known issues
deprecatedThe `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.
affects: >=1.0.2
gotchaThe `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.
affects: >=1.0.0
gotchaThe 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.
affects: >=1.0.0
breakingThis 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).
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
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.
TypeError: appRootDir.get is not a function
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.
Returned root directory is incorrect or unexpected (e.g., '/usr/local/bin' or user home directory)
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.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
10
OpenAI (training)
1
Resources
app-root-dir — npm install app-root-dir · libregistry