# detect-import-require

> list require and import paths from a JavaScript source

Latest version **2.0.0** (published 2016-06-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install detect-import-require
pnpm add detect-import-require
yarn add detect-import-require
bun add detect-import-require
```

## 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 | 2.0.0 |
| Published | 2016-06-13 |
| First published | 2015-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Matt DesLauriers |
| Maintainers | mattdesl |
| Keywords | import, cjs, common, js, commonjs, imports, require, requires, detective, node, ast, walk, find, module, modules |

## Links

- npm: https://www.npmjs.com/package/detect-import-require
- Repository: https://github.com/Jam3/detect-import-require
- Issues: https://github.com/Jam3/detect-import-require/issues
- npm.io page: https://npm.io/package/detect-import-require

## Dependencies (5)

- [acorn](https://npm.io/package/acorn.md) ^3.2.0
- [defined](https://npm.io/package/defined.md) ^1.0.0
- [ast-types](https://npm.io/package/ast-types.md) ^0.8.16
- [escodegen](https://npm.io/package/escodegen.md) ^1.7.0
- [is-buffer](https://npm.io/package/is-buffer.md) ^1.1.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2016-06-13
- 1.1.0 — 2015-11-18
- 1.0.3 — 2015-11-18
- 1.0.2 — 2015-11-18
- 1.0.1 — 2015-11-18
- 1.0.0 — 2015-11-18

## README

# detect-import-require

[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)

This is like [detective](https://www.npmjs.com/package/detective), but with a narrow focus and thin API, specifically aimed at supporting either `import` and/or `require` statements, and not much more.

## Install

```sh
npm install detect-import-require --save
```

## Example

Given the following file:

`source.js`
```js
var foo = require('a').foo
var bar = require('./blah.js')
import { uniq } from 'lodash'
import { resolve } from 'path'
```

```js
var fs = require('fs')
var detect = require('detect-import-require')

var src = fs.readFileSync('source.js', 'utf8')
console.log(detect(src))
//=> [ 'a', './blah.js', 'lodash', 'path' ]
```

See [Custom AST](#custom-ast) for details on parsing additional language syntax, such as JSX or async/await.

## Usage

[![NPM](https://nodei.co/npm/detect-import-require.png)](https://www.npmjs.com/package/detect-import-require)

#### `modules = detect(src, [opt])`

Returns an array of module names (require paths) from the given `src` String, Buffer or AST. By default, looks for `import` and `require` statements. Results are not de-duplicated, and are in the order they are found.

Options:

- `imports` (Boolean) - whether to look for `import` statements, default true
- `requires` (Boolean) - whether to look for `require` statements, default true

#### `modules = detect.find(src, [opt])`

Takes the same options as above, but returns an object with the following additional data:

```js
{
  strings: [],
  expressions: [],
  nodes: []
}
```

Where `strings` is the array of module names, `expressions` is an array of expressions from dynamic `require()` statements, and `nodes` is an array of AST nodes for each found require/import statement.

Expressions do not appear in imports, and look like this:

```js
[
  "path.join(__dirname, '/foo.js')",
  "__dirname + '/file.js'"
]
```

## Custom AST

You can also pass a parsed AST, e.g. if you have a special build of acorn or want full control over parsing options. Here is an example with JSX:

```js
var detect = require('detect-import-require')
var acorn = require('acorn-jsx');
var jsx = [
  "import 'foo';",
  "ReactDOM.render(",
    "<h1>Hello World</h1>,",
    "document.getElementById('root')",
  ");"
].join('\n');

var ast = acorn.parse(jsx, {
  ecmaVersion: 6,
  sourceType: 'module',
  allowReserved: true,
  allowReturnOutsideFunction: true,
  allowHashBang: true,
  plugins: {
    jsx: true
  }
})

detect(ast)
// => [ 'foo' ]
```

## License

MIT, see [LICENSE.md](http://github.com/Jam3/detect-import-require/blob/master/LICENSE.md) for details.

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