├── .gitignore ├── README.md ├── override-core-modules ├── fs.js ├── index.js └── my-fs.js ├── require-folder ├── data.js ├── data │ └── index.js └── index.js ├── require-json ├── data.js ├── data.json └── index.js ├── require-module-files ├── global │ └── index.js ├── index.js └── submodules │ ├── a.js │ ├── b.js │ ├── c.js │ └── index.js ├── require-names ├── index.js └── remove.js ├── require-nested-method ├── data.txt └── index.js └── require-order ├── index.js └── mod.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js require(s) best practices 2 | 3 | Some best practices / conventions when using Node's `require()`. 4 | 5 | ##### 1. [Order](https://github.com/zeMirco/node-require-s--best-practices/blob/master/require-order/index.js) 6 | 7 | Have a consistent order when requiring modules. 8 | 9 | - core modules first 10 | - then public ones from npm / `node_modules` 11 | - then your own ones 12 | 13 | ```js 14 | // 1. core modules 15 | var fs = require('fs'); 16 | var http = require('http'); 17 | 18 | // 2. public modules from npm 19 | var express = require('express'); 20 | var uuid = require('node-uuid'); 21 | 22 | // 3. your own modules from file 23 | var myMod = require('./mod.js'); 24 | ``` 25 | 26 | ##### 2. [Naming](https://github.com/zeMirco/node-require-s--best-practices/blob/master/require-names/index.js) 27 | 28 | Give corresponding variable names. 29 | 30 | ```js 31 | // don't 32 | var save = require('./remove'); 33 | 34 | // do 35 | var remove = require('./remove'); 36 | ``` 37 | 38 | ##### 3. [JSON](https://github.com/zeMirco/node-require-s--best-practices/blob/master/require-json/index.js) 39 | 40 | Include `.json` in filename. 41 | 42 | ```js 43 | // don't 44 | var data = require('./data'); 45 | 46 | // do 47 | var data = require('./data.json'); 48 | ``` 49 | 50 | ##### 4. [Folder](https://github.com/zeMirco/node-require-s--best-practices/blob/master/require-folder/index.js) 51 | 52 | Append slash when requiring `./FOLDER/index.js` 53 | 54 | ```js 55 | // don't 56 | var data = require('./data'); 57 | 58 | // do 59 | var data = require('./data/'); 60 | ``` 61 | 62 | ##### 5. [Override core modules](https://github.com/zeMirco/node-require-s--best-practices/blob/master/override-core-modules/index.js) 63 | 64 | Do not override core modules. 65 | 66 | ```js 67 | // don't 68 | var fs = require('./fs'); 69 | 70 | // do 71 | var fs = require('fs'); 72 | var myFs = require('./fs'); 73 | 74 | // or even better 75 | var fs = require('fs'); 76 | var myFs = require('./my-fs'); 77 | ``` 78 | 79 | ##### 6. [Nested objects](https://github.com/zeMirco/node-require-s--best-practices/blob/master/require-nested-method/index.js) 80 | 81 | Require module first and then create shortcut. 82 | 83 | ```js 84 | // don't 85 | var readFile = require('fs').readFile; 86 | 87 | // do 88 | var fs = require('fs'); 89 | var readFile = fs.readFile; 90 | ``` 91 | 92 | 93 | ##### 7. [Module files](https://github.com/zeMirco/node-require-s--best-practices/blob/master/require-module-files/index.js) 94 | 95 | Organise your modules so that you can require sub-files 96 | 97 | ```js 98 | // don't 99 | var isoblend = require("isoblend") 100 | var normal = isoblend.normal 101 | 102 | // do 103 | normal = require("isoblend/normal") 104 | ``` 105 | 106 | ## Imprint 107 | 108 | These are my own best practices and not related to Joyent or Node.js. 109 | 110 | ## License 111 | 112 | Copyright (C) 2014 [Mirco Zeiss](mailto: mirco.zeiss@gmail.com) 113 | 114 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 115 | 116 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 117 | 118 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 119 | -------------------------------------------------------------------------------- /override-core-modules/fs.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zemirco/node-require-s--best-practices/14bdb0407df52ff241e1cb1f6ad9c780e2bd10f4/override-core-modules/fs.js -------------------------------------------------------------------------------- /override-core-modules/index.js: -------------------------------------------------------------------------------- 1 | 2 | // don't 3 | var fs = require('./fs'); 4 | 5 | // do 6 | var fs = require('fs'); 7 | var myFs = require('./fs'); 8 | 9 | // or even better 10 | var fs = require('fs'); 11 | var myFs = require('./my-fs'); -------------------------------------------------------------------------------- /override-core-modules/my-fs.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zemirco/node-require-s--best-practices/14bdb0407df52ff241e1cb1f6ad9c780e2bd10f4/override-core-modules/my-fs.js -------------------------------------------------------------------------------- /require-folder/data.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = { 3 | text: 'some text from data.js file' 4 | }; -------------------------------------------------------------------------------- /require-folder/data/index.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | module.exports = { 4 | text: 'some text from data/index.js file' 5 | }; -------------------------------------------------------------------------------- /require-folder/index.js: -------------------------------------------------------------------------------- 1 | 2 | // what do you think is required? 3 | // data.js or data/index.js? 4 | var data = require('./data'); 5 | console.log(data.text); // logs 'some text from data.js file' 6 | 7 | // we've learned [NAME].js > [NAME]/index.js 8 | 9 | /** 10 | * Let's make it clear 11 | */ 12 | 13 | // put slash at the end to require the folder 14 | data = require('./data/'); 15 | console.log(data.text); // logs 'some text from data/index.js file' -------------------------------------------------------------------------------- /require-json/data.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = { 3 | text: 'some text from .js file' 4 | }; -------------------------------------------------------------------------------- /require-json/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "text": "some text from .json file" 3 | } -------------------------------------------------------------------------------- /require-json/index.js: -------------------------------------------------------------------------------- 1 | 2 | // what do you think is required? 3 | // .json or .js file? 4 | var data = require('./data'); 5 | console.log(data.text); // logs 'some text from .js file' 6 | 7 | // we've learned .js > .json 8 | 9 | /** 10 | * Let's make it clear 11 | */ 12 | 13 | data = require('./data.json'); 14 | console.log(data.text); // logs 'some text from .json file' -------------------------------------------------------------------------------- /require-module-files/global/index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = { 3 | a: function() { 4 | console.log('some text from global module a'); 5 | }, 6 | b: function() { 7 | console.log('some text from global module b'); 8 | } 9 | }; -------------------------------------------------------------------------------- /require-module-files/index.js: -------------------------------------------------------------------------------- 1 | // don't 2 | var global = require("./global/"); 3 | var a_glob = global.a; 4 | var b_glob = global.b; 5 | 6 | a_glob(); 7 | b_glob(); 8 | 9 | // do 10 | var submodules = require('./submodules/'); 11 | var a_sub = submodules.a; 12 | var b_sub = submodules.b; 13 | 14 | a_sub(); 15 | b_sub(); 16 | 17 | // do alternatively 18 | var a_direct = require('./submodules/a'); 19 | 20 | a_direct(); -------------------------------------------------------------------------------- /require-module-files/submodules/a.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function() { 3 | console.log('some text from module a'); 4 | }; -------------------------------------------------------------------------------- /require-module-files/submodules/b.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function() { 3 | console.log('some text from module b'); 4 | }; -------------------------------------------------------------------------------- /require-module-files/submodules/c.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function() { 3 | console.log('some text from module c'); 4 | }; -------------------------------------------------------------------------------- /require-module-files/submodules/index.js: -------------------------------------------------------------------------------- 1 | 2 | exports.a = require('./a'); 3 | exports.b = require('./b'); 4 | exports.c = require('./c'); -------------------------------------------------------------------------------- /require-names/index.js: -------------------------------------------------------------------------------- 1 | 2 | // don't 3 | var save = require('./remove'); 4 | 5 | // do 6 | var remove = require('./remove'); -------------------------------------------------------------------------------- /require-names/remove.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zemirco/node-require-s--best-practices/14bdb0407df52ff241e1cb1f6ad9c780e2bd10f4/require-names/remove.js -------------------------------------------------------------------------------- /require-nested-method/data.txt: -------------------------------------------------------------------------------- 1 | some text from data.txt -------------------------------------------------------------------------------- /require-nested-method/index.js: -------------------------------------------------------------------------------- 1 | 2 | // don't 3 | var readFile_wrong = require('fs').readFile; 4 | 5 | // do 6 | var fs = require('fs'); 7 | var readFile = fs.readFile; 8 | 9 | // works both 10 | readFile_wrong('./data.txt', 'utf8', function(err, text) { 11 | if (err) console.log(err); 12 | console.log(text); 13 | }); 14 | 15 | readFile('./data.txt', 'utf8', function(err, text) { 16 | if (err) console.log(err); 17 | console.log(text); 18 | }); -------------------------------------------------------------------------------- /require-order/index.js: -------------------------------------------------------------------------------- 1 | 2 | // 1. core modules 3 | var fs = require('fs'); 4 | var http = require('http'); 5 | 6 | // 2. public modules from npm 7 | var express = require('express'); 8 | var uuid = require('node-uuid'); 9 | 10 | // 3. your own modules from file 11 | var myMod = require('./mod.js'); -------------------------------------------------------------------------------- /require-order/mod.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zemirco/node-require-s--best-practices/14bdb0407df52ff241e1cb1f6ad9c780e2bd10f4/require-order/mod.js --------------------------------------------------------------------------------