Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

get-root-path

andfaulkner7.8kMIT3.0.1TypeScript support: included

Get the closest parent folder containing a package.json file

path, root, root-path, boilerplate, config, setup, directory-structure, file, directory, load, import, require, convenience, low-level

readme

Get Root Path - NodeJS

  • Stop counting dots - get the project root path with a single import
    • (...or a single require if you're oldskool)
  • Memoizes the result for rapid lookups after initial run

What is the root path?

  • The nearest directory up the file system tree with a package.json file.

Installation

npm install --save get-root-path

Basic usage

import { rootPath } from 'get-root-path';

path.join(rootPath, 'build/app/client');
  • If you set the APP_ROOT_PATH environment variable, it will return the value of APP_ROOT_PATH as the root path instead.

Real-world usage

import { rootPath } from 'get-root-path';

const app = express()
    .use('/', express.static(path.join(rootPath, 'build/app/client')));

What it does

  • No magic: it just finds the nearest parent (ancestor?) directory containing a package.json file
    • 99% of the time this will be the project root of the file doing the lookup.

changelog

v3.0.1

  • [DOCS] Update README - remove unneeded descriptions

  • [CHORE] Upgrade default node.js version from 6 to 22

  • [CHORE] Upgrade Typescript version to 4.9.5

v3.0.0

  • [BREAKING] Only checks for the presence of a package.json file to determine if a location is a root path

    • node_modules folder is no longer required
    • Accounts for:
      • Projects with no dependencies;
      • Newly pulled repositories; and
      • Repositories that have just run a build step that (temporarily) removed node_modules (e.g. a "clean" step)
  • [DOCS] Add CHANGELOG

  • [DOCS] Add installation instructions to README
  • [DOCS] Fix import instructions in README

  • [CHORE] Remove @types/lodash package