Last active
May 13, 2022 10:38
Revisions
-
aprilmintacpineda revised this gist
Jan 7, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -52,5 +52,5 @@ You can now use `_require` for your `local modules`, and you can still use `requ 1. This is not a hacky solution that suggests you mess with a built-in function of node that might change in future version, in short, it's version independent and might even work with previous versions of node with the assumption that `path` and `require` on those version works the same way, unless that version of node implements `_require` as a built-in function, if it does, then simply change the name? ¯\_(ツ)_/¯ 2. It works globally anywhere in your NodeJS app. 3. You don't have to manually do something like ```require(`${basepath}/my/module.js`)```. 4. Since we are using `path` module, it should work on other OS too. (Tell me if it doesn't, I'd like to figure out why). -
aprilmintacpineda revised this gist
Jan 7, 2020 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,4 +46,11 @@ The file structure would look like: |- constants.js ``` You can now use `_require` for your `local modules`, and you can still use `require` for your `node_modules`. ## Why use this solution? 1. This is not a hacky solution that suggests you mess with a built-in function of node that might change in future version, in short, it's version independent and might even work with previous versions of node with the assumption that `path` and `require` on those version works the same way, unless that version of node implements `_require` as a built-in function, if it does, then simply change the name? ¯\_(ツ)_/¯ 2. It works globally anywhere in your NodeJS app. 3. You don't have to manually do something like `require(`${basepath}/my/module.js`)`. 4. Since we are using `path` module, it should work on other OS too. (Tell me if it doesn't, I'd like to figure out why). -
aprilmintacpineda revised this gist
Dec 26, 2019 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,8 @@ on the top of your `server.js` or whatever the filename is of that which you run ```js const path = require('path'); global._require = module => require(path.join(__dirname, module)); //.. rest of your codes ``` Then on `a/very/far/away/module.js`: -
aprilmintacpineda revised this gist
Dec 26, 2019 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,9 +23,10 @@ global._require = module => require(path.join(__dirname, module)); Then on `a/very/far/away/module.js`: ```js // coming from /root/libs/validator const validator = _require('libs/validator'); // coming from the /root/constanst const { validListOfThings } = _require('constants'); ``` The file structure would look like: -
aprilmintacpineda revised this gist
Dec 26, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ global._require = module => require(path.join(__dirname, module)); Then on `a/very/far/away/module.js`: ```js const validator = _require('libs/validator'); // coming from the /root const { validListOfThings } = require('constants'); -
aprilmintacpineda renamed this gist
Nov 23, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
aprilmintacpineda revised this gist
Nov 23, 2019 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,16 @@ # NodeJS require concept for local modules ## The problem As your NodeJS app grows bigger, the file structure tends to go 3 to even 5 layers deep. The problem now is as you `require` local modules you created, you'll have to write them in this way: ```js const myModule = require('../../../my/module'); ``` This can become an awful developer experience. ## The simplest solution I thought of: on the top of your `server.js` or whatever the filename is of that which you run first when you startup your nodejs application. -
aprilmintacpineda created this gist
Nov 23, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ The simplest solution I thought of: on the top of your `server.js` or whatever the filename is of that which you run first when you startup your nodejs application. ```js const path = require('path'); global._require = module => require(path.join(__dirname, module)); ``` Then on `a/very/far/away/module.js`: ``` const validator = _require('libs/validator'); // coming from the /root const { validListOfThings } = require('constants'); ``` The file structure would look like: ``` /root |- a | |- very | | |- far | | | |- away | | | | |- module.js |- libs | |- validator.js |- server.js |- constants.js ``` You can now use `_require` for your `local modules`, and you can still use `require` for your `node_modules`.