# @incpad/need

> 对于普通node程序来说，由于允许随时调用require注入，所以只需要如此： ```$xslt const {need} =require("@incpad/need") const _=need("lodash") _.get({},"xxx") ``` 如果传入第二个参数，即给与指定的上下文，如果第二个参数是个对象， 便会将该库注入到对应的对象中 ```$xslt const {need} =require("@incpad/need") let test={} need("lodash",test

Latest version **1.0.22** (published 2019-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @incpad/need
pnpm add @incpad/need
yarn add @incpad/need
bun add @incpad/need
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.22 |
| Published | 2019-02-18 |
| First published | 2019-02-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 47.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | yixuankeer |

## Links

- npm: https://www.npmjs.com/package/@incpad/need
- npm.io page: https://npm.io/package/@incpad/need

## Dependencies (5)

- [lodash.curry](https://npm.io/package/lodash.curry.md) ^4.1.1
- [lodash.isnil](https://npm.io/package/lodash.isnil.md) ^4.0.0
- [lodash.isstring](https://npm.io/package/lodash.isstring.md) ^4.0.1
- [@incpad/validator](https://npm.io/package/@incpad/validator.md) ^0.0.27
- [@incpad/error-handle](https://npm.io/package/@incpad/error-handle.md) ^1.0.26

## Recent versions

- 1.0.22 (latest) — 2019-02-18
- 1.0.21 — 2019-02-13

## README

### Usage
对于普通node程序来说，由于允许随时调用require注入，所以只需要如此：
```$xslt
const {need} =require("@incpad/need")
const _=need("lodash")
_.get({},"xxx")
```
如果传入第二个参数，即给与指定的上下文，如果第二个参数是个对象，
便会将该库注入到对应的对象中
```$xslt
const {need} =require("@incpad/need")
let test={}
need("lodash",test)
console.log(test.lodash)
```

对于babel系列的程序，由于需要编译处理，所以require不可以接受变量作为参数，所以需要特定的adapter作为处理  

同时，如果想要注入自己写的js文件而不是node_modules里的模块，也需要添加对应的adapter作为处理

下面给出adapter文件示例:
```$xslt
module.exports={
    tester:moduleName=>true,
    getMod:moduleName=>mod
}
```
调用need函数时，程序遍历内部的adapter数组，将模块名传入tester函数，如果返回true，则代表该adapter可以处理该模块，  
然后程序会调用对应的getMod方法，该方法应该返回需求的模块。


通过如下方式可以将自定义adapter添加到adapter数组中(以unshift的方式):

```$xslt
const {need,addAdapter} =require("@incpad/need")
const cusAdapter=reuqire("./addAdapter.js")
addAdapter(cusAdapter)
```
如果对应adapter格式不正确，会抛出对应错误

---
_Source: https://npm.io/package/@incpad/need · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
