Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. aprilmintacpineda revised this gist Jan 7, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NodeJS require local modules resolver.md
    Original 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`)`.
    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).
  2. aprilmintacpineda revised this gist Jan 7, 2020. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion NodeJS require local modules resolver.md
    Original 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`.
    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).
  3. aprilmintacpineda revised this gist Dec 26, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions NodeJS require local modules resolver.md
    Original 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`:
  4. aprilmintacpineda revised this gist Dec 26, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions NodeJS require local modules resolver.md
    Original 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
    const { validListOfThings } = require('constants');
    // coming from the /root/constanst
    const { validListOfThings } = _require('constants');
    ```

    The file structure would look like:
  5. aprilmintacpineda revised this gist Dec 26, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NodeJS require local modules resolver.md
    Original 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');
  6. aprilmintacpineda renamed this gist Nov 23, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. aprilmintacpineda revised this gist Nov 23, 2019. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,16 @@
    The simplest solution I thought of:
    # 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.
  8. aprilmintacpineda created this gist Nov 23, 2019.
    34 changes: 34 additions & 0 deletions README.md
    Original 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`.