[node.js] How to make node.js require absolute? (instead of relative)

What I like to do is leverage how node loads from the node_module directory for this.

If one tries to load the module "thing", one would do something like

require('thing');

Node will then look for the 'thing' directory in the 'node_module' directory.

Since the node_module is normally at the root of the project, we can leverage this consistency. (If node_module is not at the root, then you have other self induced headaches to deal with.)

If we go into the directory and then back out of it, we can get a consistent path to the root of the node project.

require('thing/../../');

Then if we want to access the /happy directory, we would do this.

require('thing/../../happy');

Though it is quite a bit hacky, however I feel if the functionality of how node_modules load changes, there will be bigger problems to deal with. This behavior should remain consistent.

To make things clear, I do this, because the name of module does not matter.

require('root/../../happy');

I used it recently for angular2. I want to load a service from the root.

import {MyService} from 'root/../../app/services/http/my.service';