aichholzer / attract

Requiring libraries with love. ❤️
MIT License
2 stars 0 forks source link

attract

Greenkeeper badge Build Status Downloads Always useful

Requiring libraries with love. ❤
attract basically replaces require(), providing a more flexible way of requiring libraries and modules in your project.
(It still uses require() under the hood, which provides caching all the things you need)

Features

Install

npm i attract

Usage

Before:
const fs = require('fs');
const express = require('express');
const mongoose = require('mongoose');
After:
require('attract')();
const [
  fs,
  express,
  mongoose
] = attract('fs', 'express', 'mongoose');

attract is also registered as required, so you could use it like:

const [
  fs,
  express,
  mongoose
] = required('fs', 'express', 'mongoose');

You can also get rid of all those horrible relative path requires throughout your application, and require third-party modules easily with attract, just define your basePath at start and you are good to go.

Before:
const moduleOne = require('../../modules/path/to/moduleOne');
const moduleTwo = require('../../../modules/path/to/moduleTwo');
After:
// Set `basePath` to your project's root folder, for example.
require('attract')({ basePath: __dirname })
const [
  moduleOne,
  moduleTwo
] = attract('path/to/moduleOne', 'path/to/moduleTwo');

Contribute

fork https://github.com/aichholzer/attract/

License

MIT