app-module-path-node icon indicating copy to clipboard operation
app-module-path-node copied to clipboard

Aliasing a path?

Open IngwiePhoenix opened this issue 10 years ago • 3 comments

I really like this module, but there is something I would like to know.

Within the root of my project, i have app, config, cache and more such folders. Now, within the app folder, I have my entire app. That app is a hybrid PHP/NodeJS app. The root namespace is BIRD3\. And with WebPack, I can alias {BIRD3: "./app"} just fine.

But is there such way for NodeJS?

// ... magically alias app/ to be BIRD3
var worker = require("BIRD3/Backend/Service/JobWorker");

Kind regards, Ingwie.

IngwiePhoenix avatar Oct 27 '15 18:10 IngwiePhoenix

I haven't had a lot of time to look into this, but what are your thoughts on just using symbolic links in your project? That might be cleaner than further hacking the module loading system. Thoughts?

patrick-steele-idem avatar Oct 31 '15 20:10 patrick-steele-idem

Well I did use symbolic links for a while now, but there is a problem: NPM. It continiously asks me to set a package.json in that folder - which I don't want. the whole purpose of that, is to link my main app folder as a module - or, alias it.

app/
config/
cache/
logs/
util/
node_modules/
php_modules/
bower_components/

That is my base structure. Now, within app, I have my main application logic. So instead of using a load of relative paths, I want to just require off the root; i.e.:

var router = require("BIRD3/Backend/http/Router");
router(app); // installs routes.

I can do that using a symlink. But the pesky npm "warning" is - to quote Steve Jobs - "uuuuuugly".

npm WARN ENOENT ENOENT: no such file or directory, open '/Users/Ingwie/Work/BIRD3/app/package.json'

But, placing a package.json in there, breaks my root-trace. I use find-root to pick up where the actual root of my app is - this works by tracing the folder tree upwards, till it finds a package.json. Placing it within the application folder defeats the purpose of even using that.

So with these things in mind, you may see why I was asking for support of aliasing a folder. :)

IngwiePhoenix avatar Nov 06 '15 16:11 IngwiePhoenix

Hey @IngwiePhoenix, this module only changes how the Node.js module search paths are constructed. It doesn't change how modules are resolved which is what would be required for your use case.

Here's another idea:

Introduce your own global requireAlias(path) function:

var nodePath = require('path');

function registerAliases(aliases) {
    global.requireAlias = function(path) {
        var parts = path.split(/[\\/]/);
        var alias = parts[0];
        var aliasDir = aliases[alias];
        var remaining = parts.slice(1).join('/');
        var resolved = nodePath.join(aliasDir, remaining);
        return require(resolved);
    }
}

registerAliases({
    'FOO': nodePath.join(__dirname, 'app')
});

var test = requireAlias('FOO/test.js');
test.sayHello();

Does that work for you? It's kind of a hack but it is probably better to do do that instead of trying to hack the Node.js module loading system. The problem is that if you are using a JavaScript module bundler then it won't be able to detect the requires that are done using requireAlias(). As long as your code is running on the server then you would be good to go.

patrick-steele-idem avatar Nov 06 '15 23:11 patrick-steele-idem