{"id":12816,"library":"app-root-dir","title":"App Root Dir","description":"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.","status":"abandoned","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/philidem/node-app-root-dir","tags":["javascript","modules","path","node","app","root","directory"],"install":[{"cmd":"npm install app-root-dir","lang":"bash","label":"npm"},{"cmd":"yarn add app-root-dir","lang":"bash","label":"yarn"},{"cmd":"pnpm add app-root-dir","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and does not support ESM imports.","wrong":"import appRootDir from 'app-root-dir';","symbol":"appRootDir","correct":"const appRootDir = require('app-root-dir');"},{"note":"Access the `get` method directly from the CommonJS module export.","wrong":"import { get } from 'app-root-dir';","symbol":"get","correct":"const rootPath = require('app-root-dir').get();"},{"note":"The `set` method modifies a global variable, affecting all instances of the module, which can be a source of unexpected behavior.","wrong":"import { set } from 'app-root-dir';","symbol":"set","correct":"require('app-root-dir').set(__dirname);"}],"quickstart":{"code":"const appRootDir = require('app-root-dir');\nconst path = require('path');\n\n// Get the inferred application's root directory\nconst currentRootDir = appRootDir.get();\nconsole.log(`Current inferred root directory: ${currentRootDir}`);\n\n// Manually set the application's root directory (this sets a global state)\n// This should typically be done early in your application's lifecycle.\n// For demonstration, we'll set it to a mock path.\nconst mockAppPath = path.resolve(process.cwd(), 'temp_app_root');\nconsole.log(`Setting application root directory to: ${mockAppPath}`);\nappRootDir.set(mockAppPath);\n\n// Verify the change\nconst newRootDir = appRootDir.get();\nconsole.log(`New inferred root directory after setting: ${newRootDir}`);\n\n// Reset for further demonstration or testing (optional, for cleanup)\n// In a real app, you might not reset it like this.\n// Note: There is no direct 'unset' or 'clear' function.\n// The only way to revert is to set it to another value.\n// For simplicity in this example, we will not 'unset' it.","lang":"javascript","description":"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."},"warnings":[{"fix":"Consider migrating to actively maintained alternatives like `app-root-path` which offer better compatibility and features.","message":"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.","severity":"deprecated","affected_versions":">=1.0.2"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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).","message":"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.","severity":"breaking","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to use `require('app-root-dir')` in an ES module context.","error":"ReferenceError: require is not defined"},{"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.","cause":"The module was not correctly imported or resolved, leading to `appRootDir` being undefined or not the expected module object.","error":"TypeError: appRootDir.get is not a function"},{"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.","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).","error":"Returned root directory is incorrect or unexpected (e.g., '/usr/local/bin' or user home directory)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null}