JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3496574
  • Score
    100M100P100Q195795F
  • License MIT

Determine an app's root path from anywhere inside the app

Package Exports

  • app-root-path

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (app-root-path) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

App Root Path Module

This simple module helps you access your application's root path from anywhere in the application without resorting to require("../../path").

Installation

  $ npm install app-root-path --save

Usage

To simply access the app's root path:

    var appRoot = require('app-root-path'),
        myModule = require(appRoot + '/lib/my-module.js');

A helper function is also provided:

    var appRequire = require('app-root-path').require(require),
        myModule = appRequire('/lib/my-module.js');

This works by passing the current module's require method (each module has its own require method) to the app-root-path module, which then returns a wrapper for that method that prepends the application's root path to whatever path is passed to it.

Finally, you can also just resolve a module path:

    var myModulePath = require('app-root-path').resolve('/lib/my-module.js');

How It Works

This module works on the assumption that your application's root path is the parent of the node_modules directory. Here's almost all the module's logic:

    var appRootPath = path.resolve(__dirname, '..', '..');

So, given this directory structure:

my-app                  <--  ..
    node_modules        <--  ..
        app-root-path   <--  __dirname
            index.js
    lib
    my-module.js
        index.js

This may not work in every case--particularly if you try to use this in a module that is then used by other modules--but it should work 99% of the time when you're using it within the main application.