Home / Blog / Introducing elquire: local Node dependencies without the hassle

Introducing elquire: local Node dependencies without the hassle

By Sam Gluck, 02 Nov 2015

Check out elquire on GitHub or npm.

I love Node. I love building Node applications. I don’t love mentally resolving a relative path to a local dependency each time I create a new file or a file is moved to a new position in the application. So I built elquire.

Local Node dependencies without the hassle: require by a given name instead of a relative path.

elquire allows you to name any javascript file in your application and require it by that name, never having to deal with writing relative paths. Let’s take a look at an example of how elquire can help us.

Initialise elquire:

In an entry file, initialise elquire with the desired options:

require('elquire')({
    namespace: 'myApp',
    path: './server'
});
require('./server');

Without elquire:

let emailService = require('../../../services/workers/email/emailService.js');

With elquire:

let emailService = require('myApp.emailService');

If you’re currently using NODE_PATH in your application and aren’t yet convinced of elquire’s potential, here are some advantages to using elquire that might change your mind:

  • It is not clear that a module loaded via NODE_PATH is actually a local dependency unless you place all local dependencies within a common directory so that they appear namespaced. elquire allows naming and namepacing that is independent of the application structure.
  • In large applications the position of local dependencies means that you might end up with paths like lib/services/workers/aWorker.js. With elquire you might see something like myApp.workers.aWorker, which is more readable and less ambiguous – it uses a dot path separator instead of a forward-slash, which is used for external (npm) modules.
  • You are not required to modify an environment variable or a path when moving a local module from one directory to another. This is particularly time-saving if you are making large changes to the structure of your application.
  • You are not forced to name each file index.js and place it within its own named directory in order to require it ‘by name’ (e.g. lib/services/workers/aWorker).

We have started using elquire in our applications. We hope you do too. We welcome any and all feedback, pull requests, issues, etc.

– Sam