├── .gitignore ├── .jshintrc ├── LICENSE ├── README.md ├── lib └── index.js ├── node-app-root-dir.sublime-project └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "globals": { 3 | "require": true 4 | }, 5 | 6 | "node" : true, 7 | "es5" : false, 8 | "browser" : true, 9 | "boss" : false, 10 | "curly": false, 11 | "debug": false, 12 | "devel": false, 13 | "eqeqeq": true, 14 | "evil": true, 15 | "forin": false, 16 | "immed": true, 17 | "laxbreak": false, 18 | "newcap": true, 19 | "noarg": true, 20 | "noempty": false, 21 | "nonew": true, 22 | "nomen": false, 23 | "onevar": false, 24 | "plusplus": false, 25 | "regexp": false, 26 | "undef": true, 27 | "sub": false, 28 | "white": false, 29 | "eqeqeq": false, 30 | "latedef": true, 31 | "unused": "vars", 32 | "strict": false, 33 | 34 | /* Relaxing options: */ 35 | "eqnull": true 36 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Phillip Gates-Idem 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-app-root-dir 2 | ================= 3 | 4 | Simple module to infer the root directory of the currently running node application 5 | 6 | ## Usage 7 | ```javascript 8 | // get the application's root directory 9 | var appRootDir = require('app-root-dir').get(); 10 | 11 | // set the application's root directory 12 | // (this will set a global so that no matter 13 | // how many instances of app-root-dir module are 14 | // installed, they will all return the same 15 | // directory) 16 | require('app-root-dir').set(__dirname); 17 | ``` 18 | 19 | 20 | ## How it Works 21 | The following strategy is used to find the application's root directory (the directory in your project that contains the main package.json file): 22 | 23 | * If **package.json** exists at `process.cwd()` then use `process.cwd()` as the application root directory. 24 | * Else if, the **app-root-dir** module has **node_modules** directory in its path then use the directory above this as the application root directory. NOTE: The parent directory of the _first_ **node_modules** directory in the path is used if the **app-root-dir** module is installed as a submodule of another module. 25 | * Else, use the directory of **app-root-dir** module as the application root directory. 26 | 27 | For example, consider this directory structure for the scenarios below: 28 | + my-project 29 | + package.json 30 | + server.js 31 | + node_modules 32 | + app-root-dir 33 | + lib 34 | + index.js 35 | 36 | ### Scenario 1: 37 | Application is ran as: 38 | `node server.js` 39 | 40 | The application root directory will be **my-project** because **package.json** exists at `process.cwd()` 41 | 42 | ### Scenario 2: 43 | Application is ran as: 44 | `node my-project/server.js` 45 | 46 | There is no **package.json** at `process.cwd()`. The application root directory will still be **my-project** because **my-project/node_modules/app-root-dir/lib/index.js** has **node_modules** in its path and the directory above **node_modules** is the application's root directory. 47 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var GLOBAL_KEY = 'app-root-dir'; 2 | var _rootDir; 3 | 4 | exports.get = function() { 5 | var dir = global[GLOBAL_KEY]; 6 | if (dir) { 7 | return dir; 8 | } 9 | 10 | if (_rootDir === undefined) { 11 | var fs = require('fs'); 12 | var path = require('path'); 13 | var NODE_MODULES = path.sep + 'node_modules' + path.sep; 14 | var cwd = process.cwd(); 15 | var pos = cwd.indexOf(NODE_MODULES); 16 | if (pos !== -1) { 17 | _rootDir = cwd.substring(0, pos); 18 | } else if (fs.existsSync(path.join(cwd, 'package.json'))) { 19 | _rootDir = cwd; 20 | } else { 21 | pos = __dirname.indexOf(NODE_MODULES); 22 | if (pos === -1) { 23 | _rootDir = path.normalize(path.join(__dirname, '..')); 24 | } else { 25 | _rootDir = __dirname.substring(0, pos); 26 | } 27 | } 28 | } 29 | 30 | return _rootDir; 31 | }; 32 | 33 | exports.set = function(dir) { 34 | global[GLOBAL_KEY] = _rootDir = dir; 35 | }; 36 | -------------------------------------------------------------------------------- /node-app-root-dir.sublime-project: -------------------------------------------------------------------------------- 1 | { 2 | "folders": 3 | [ 4 | { 5 | "follow_symlinks": true, 6 | "path": "." 7 | } 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "app-root-dir", 3 | "description": "Simple module to infer the root directory of the currently running node application", 4 | "main": "lib/index.js", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/philidem/node-app-root-dir.git" 8 | }, 9 | "keywords": [ 10 | "modules", 11 | "path", 12 | "node", 13 | "app", 14 | "root", 15 | "directory" 16 | ], 17 | "author": "Phillip Gates-Idem ", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/philidem/node-app-root-dir/issues" 21 | }, 22 | "homepage": "https://github.com/philidem/node-app-root-dir", 23 | "publishConfig": { 24 | "registry": "https://registry.npmjs.org/" 25 | }, 26 | "version": "1.0.2" 27 | } --------------------------------------------------------------------------------