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 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.
29
+
It's a little hacky, but you can also put this method on your application's `global` object:
30
+
31
+
```js
32
+
// In app.js
33
+
global.reqlib=require('app-root-path').require;
34
+
35
+
// In lib/module/component/subcomponent.js
36
+
var myModule =reqlib('/lib/my-module.js');
37
+
```
28
38
29
39
Finally, you can also just resolve a module path:
30
40
31
41
```js
32
-
var myModulePath =require('app-root-path').resolve('/lib/my-module.js');
42
+
var myModulePath =require('app-root-path').resolve('/lib/my-module.js');
33
43
```
34
44
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')`
46
+
35
47
## How It Works
36
48
37
-
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:
49
+
This module uses two different methods to determine the app's root path, depending on the circumstances.
50
+
51
+
### Method One (preferred)
52
+
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:
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.
60
+
61
+
### Method Two (for edge cases)
62
+
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:
64
+
65
+
-`$HOME/.node_modules`
66
+
-`$HOME/.node_libraries`
67
+
-`$PREFIX/lib/node`
68
+
69
+
Or, anywhere in the `NODE_PATH` environmental variable ([see documentation](http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders)).
70
+
71
+
In these cases, we fall back to an alternate trick:
38
72
39
73
```js
40
-
var appRootPath =path.resolve(__dirname, '..', '..');
74
+
path.dirname(require.main.filename);
41
75
```
42
76
43
-
So, given this directory structure:
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.
78
+
79
+
## Change Log
44
80
45
-
my-app <-- ..
46
-
node_modules <-- ..
47
-
app-root-path <-- __dirname
48
-
index.js
49
-
lib
50
-
my-module.js
51
-
index.js
81
+
### 0.1.0
82
+
- Completely rewrote the path resolution method to account for most possible scenarios. This shouldn't cause and backwards compatibility issues, but always test your code.
83
+
- Removed the need to pass a modules's `require()` method to the `appRootPath.require()` function. Which it's true that each module has its own `require()` method, in practice it doesn't matter, and it's **much** simpler this way.
84
+
- Added tests
52
85
53
-
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.
0 commit comments