Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aichholzer/attract

Requiring libraries with love. ❤️
https://github.com/aichholzer/attract

Last synced: 25 days ago
JSON representation

Requiring libraries with love. ❤️

Awesome Lists containing this project

README

        

# attract

[![Greenkeeper badge](https://badges.greenkeeper.io/aichholzer/attract.svg)](https://greenkeeper.io/)
[![Build Status](https://travis-ci.org/aichholzer/attract.svg?branch=master)](https://travis-ci.org/aichholzer/attract)
[![Downloads](https://img.shields.io/npm/dt/attract.svg)](https://www.npmjs.com/package/attract)
[![Always useful](https://img.shields.io/badge/always-useful-ff6400.svg)](https://github.com/aichholzer/attract)

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

* Require `attract` only once, at your application's start, and it will be globally available, everywhere.
* Say goodbye to those ugly relative paths.

### Install
```
npm i attract
```

### Usage

##### Before:
```js
const fs = require('fs');
const express = require('express');
const mongoose = require('mongoose');
```

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

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

```js
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:
```js
const moduleOne = require('../../modules/path/to/moduleOne');
const moduleTwo = require('../../../modules/path/to/moduleTwo');
```

##### After:
```js
// 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](https://github.com/aichholzer/attract/blob/master/LICENSE)