You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This simple module helps you access your application's root path from anywhere in the application without resorting to `require("../../path")`.
5
+
> **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.
6
+
7
+
This simple module helps you access your application's root path from anywhere in the application without resorting to relative paths like `require("../../path")`.
To simply access the app's root path, use the module as though it were a string:
16
18
17
19
```js
18
20
var appRoot =require('app-root-path');
@@ -26,7 +28,7 @@ var reqlib = require('app-root-path').require;
26
28
var myModule =reqlib('/lib/my-module.js');
27
29
```
28
30
29
-
It's a little hacky, but you can also put this method on your application's `global` object:
31
+
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:
30
32
31
33
```js
32
34
// In app.js
@@ -42,25 +44,27 @@ Finally, you can also just resolve a module path:
42
44
var myModulePath =require('app-root-path').resolve('/lib/my-module.js');
43
45
```
44
46
45
-
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')`
47
+
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')`
48
+
49
+
## How It Works (under the hood)
46
50
47
-
## How It Works
51
+
> No need to read this unless your curious—or you run into a (very unlikely) case where the module does not work as expected.
48
52
49
53
This module uses two different methods to determine the app's root path, depending on the circumstances.
50
54
51
-
### Method One (preferred)
55
+
### Primary Method
52
56
53
-
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:
57
+
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):
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.
63
+
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.
60
64
61
-
### Method Two (for edge cases)
65
+
### Secondary Method (for edge cases)
62
66
63
-
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:
67
+
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:
64
68
65
69
-`$HOME/.node_modules`
66
70
-`$HOME/.node_libraries`
@@ -74,10 +78,13 @@ In these cases, we fall back to an alternate trick:
74
78
path.dirname(require.main.filename);
75
79
```
76
80
77
-
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.
81
+
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.
78
82
79
83
## Change Log
80
84
85
+
### 1.0.0
86
+
- No changes. Just updated the version to signify a locked API (see [semver](http://semver.org/)).
87
+
81
88
### 0.1.1
82
89
- Added Windows support (and, theoretically, other operating systems that have a directory separator that's not "/")
0 commit comments