You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB

7 years ago
# requireable
> Enables a module to require itself by name
7 years ago
[![Build Status](https://travis-ci.org/lukechilds/requireable.svg?branch=master)](https://travis-ci.org/lukechilds/requireable)
[![Coverage Status](https://coveralls.io/repos/github/lukechilds/requireable/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/requireable?branch=master)
[![npm](https://img.shields.io/npm/v/requireable.svg)](https://www.npmjs.com/package/requireable)
Mocks the require command to include the current package when the `name` from the current `package.json` is required.
Uses `process.cwd()` to find the current `package.json` so this is only meant to be used as a dev dependency for testing. This will not work on a published package!
7 years ago
## Install
```shell
npm install --save-dev requireable
```
## Usage
```js
require('requirable');
7 years ago
// That's it! `require` has now been patched
7 years ago
const myPackage = require('package-name')
```
## AVA Usage
7 years ago
Some tests runners such as [AVA](https://github.com/avajs/ava) can require packages automatically for you. If you're using AVA add the following to your package.json:
```json
"ava": {
"require": [
"requireable"
]
},
```
7 years ago
You can now `require` (or `import`) your package in your AVA tests by name.
e.g instead of:
```js
import test from 'ava';
import myPackage from '../';
```
You can now do:
```js
import test from 'ava';
import myPackage from 'package-name';
```
## API
### require('requireable')
Patches the require function to be able to require the current module by name.
Returns an object.
#### obj.success
Type: `boolean`
True or false depending on whether we could find `package.json`.
#### obj.path
Type: `undefined`, `string`
Path to the folder containing `package.json`.
#### obj.name
Type: `undefined`, `string`
Name of the module in `package.json`.
7 years ago
## License
MIT © Luke Childs