Skip to content

Commit

Permalink
Bumped version to 1.0.0 + updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Nov 7, 2014
1 parent 8fd48b2 commit 4c931e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
31 changes: 19 additions & 12 deletions README.md
@@ -1,8 +1,10 @@
# App Root Path Module

[![Build Status](https://travis-ci.org/inxilpro/node-app-root-path.svg)](https://travis-ci.org/inxilpro/node-app-root-path)
[![Build Status](https://travis-ci.org/inxilpro/node-app-root-path.svg)](https://travis-ci.org/inxilpro/node-app-root-path) [![Dependency Status](https://david-dm.org/inxilpro/node-app-root-path.svg)](https://david-dm.org/inxilpro/node-app-root-path)

This simple module helps you access your application's root path from anywhere in the application without resorting to `require("../../path")`.
> **Please Note:** Due to the very limited scope of this module, I do not anticipate needing to make very many changes to it. Expect long stretches of zero updates—that does not mean that the module is outdated.
This simple module helps you access your application's root path from anywhere in the application without resorting to relative paths like `require("../../path")`.

## Installation

Expand All @@ -12,7 +14,7 @@ $ npm install app-root-path --save

## Usage

To simply access the app's root path:
To simply access the app's root path, use the module as though it were a string:

``` js
var appRoot = require('app-root-path');
Expand All @@ -26,7 +28,7 @@ var reqlib = require('app-root-path').require;
var myModule = reqlib('/lib/my-module.js');
```

It's a little hacky, but you can also put this method on your application's `global` object:
It's a little hacky, but you can also put this method on your application's `global` object to use it everywhere in your project:

``` js
// In app.js
Expand All @@ -42,25 +44,27 @@ Finally, you can also just resolve a module path:
var myModulePath = require('app-root-path').resolve('/lib/my-module.js');
```

You can also explicitly set the path, using the environmental variable `APP_ROOT_PATH` or by calling `require('app-root-path').setPath('/my/app/is/here')`
You can explicitly set the path, using the environmental variable `APP_ROOT_PATH` or by calling `require('app-root-path').setPath('/my/app/is/here')`

## How It Works (under the hood)

## How It Works
> No need to read this unless your curious—or you run into a (very unlikely) case where the module does not work as expected.
This module uses two different methods to determine the app's root path, depending on the circumstances.

### Method One (preferred)
### Primary Method

If the module is located inside your project's directory, somewhere within the `node_modules` directory (whether directly, or inside a submodule), we just do:
If the module is located inside your project's directory, somewhere within the `node_modules` directory (whether directly, or inside a submodule), we effectively do (the actual code takes cross-platform path names/etc into consideration):

``` js
path.resolve(__dirname).split('/node_modules')[0];
```

This will take a path like `/var/www/node_modules/submodule/node_modules/app-root-path` and return `/var/www`. In 99% of cases, this is just what you need.
This will take a path like `/var/www/node_modules/submodule/node_modules/app-root-path` and return `/var/www`. In nearly all cases, this is just what you need.

### Method Two (for edge cases)
### Secondary Method (for edge cases)

The node module loader will also look in a few other places for modules (for example, ones that you install globally with `npm install -g`). Theses can be in one of:
The node module loader will also look in a few other places for modules (for example, ones that you install globally with `npm install -g`). These can be in one of:

- `$HOME/.node_modules`
- `$HOME/.node_libraries`
Expand All @@ -74,10 +78,13 @@ In these cases, we fall back to an alternate trick:
path.dirname(require.main.filename);
```

When a file is run directly from Node, `require.main` is set to its `module`. `module.filename` refers to the filename of that module, so by fetching the directory name for that file, we at least get the directory of the file that was called directly. In some cases (process managers and test suites, for example) this doesn't actually give the correct directory, though, so this method is only used as a fallback.
When a file is run directly from Node, `require.main` is set to that file's `module`. Each module has a `filename` property that refers to the filename of that module, so by fetching the directory name for that file, we at least get the directory of file passed to `node`. In some cases (process managers and test suites, for example) this doesn't actually give the correct directory, though, so this method is only used as a fallback.

## Change Log

### 1.0.0
- No changes. Just updated the version to signify a locked API (see [semver](http://semver.org/)).

### 0.1.1
- Added Windows support (and, theoretically, other operating systems that have a directory separator that's not "/")

Expand Down
12 changes: 10 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "app-root-path",
"version": "0.1.1",
"version": "1.0.0",
"description": "Determine an app's root path from anywhere inside the app",
"main": "index.js",
"scripts": {
Expand All @@ -12,7 +12,15 @@
},
"keywords": [
"root",
"path"
"path",
"utility",
"util",
"node",
"module",
"modules",
"node_modules",
"require",
"app"
],
"author": "Chris Morrell <http://cmorrell.com>",
"license": "MIT",
Expand Down

0 comments on commit 4c931e9

Please sign in to comment.