| 1 |
[![Build status][nix-build-image]][nix-build-url] |
| 2 |
[![Windows status][win-build-image]][win-build-url] |
| 3 |
![Transpilation status][transpilation-image] |
| 4 |
[![npm version][npm-image]][npm-url] |
| 5 |
|
| 6 |
# find-requires – Find all require() calls. |
| 7 |
|
| 8 |
Made for [modules-webmake](https://github.com/medikoo/modules-webmake). Fast [esniff](https://github.com/medikoo/esniff#esniff) based implementation of require calls parser. |
| 9 |
|
| 10 |
## Example |
| 11 |
|
| 12 |
foo.js: |
| 13 |
|
| 14 |
```javascript |
| 15 |
var one = require("one"); |
| 16 |
var two = require("two"); |
| 17 |
var slp = require("some/long" + "/path"); |
| 18 |
var wrong = require(cannotTakeThat); |
| 19 |
``` |
| 20 |
|
| 21 |
program.js: |
| 22 |
|
| 23 |
```javascript |
| 24 |
var fs = require("fs"); |
| 25 |
var findRequires = require("find-requires"); |
| 26 |
|
| 27 |
var src = fs.readFileSync("foo.js", "utf-8"); |
| 28 |
|
| 29 |
console.log(findRequires(src)); // => ['one', 'two', 'some/long/path']; |
| 30 |
|
| 31 |
// or we can get more detailed data with `raw` option: |
| 32 |
console.log(findRequires(src, { raw: true })); /* => [ |
| 33 |
{ value: 'one', raw: '\'one\'', point: 19, line: 1, column: 19 }, |
| 34 |
{ value: 'two', raw: '\'two\'', point: 45, line: 2, column: 19 }, |
| 35 |
{ value: 'some/long/path', raw: '\'some/long\' +\n\t\t\t\t\t\t\'/path\'', |
| 36 |
point: 71, line: 3, column: 19 }, |
| 37 |
{ raw: 'cannotTakeThat', point: 121, line: 5, column: 21 } |
| 38 |
] */ |
| 39 |
|
| 40 |
// We can also ensure some specific cases of dynamic requires code with some setup code injection |
| 41 |
console.log( |
| 42 |
findRequires("require(__dirname + '/foo.js')", { |
| 43 |
setupCode: `const __dirname = ${ JSON.stringify(__dirname) }` |
| 44 |
}) |
| 45 |
); |
| 46 |
``` |
| 47 |
|
| 48 |
## CLI Example |
| 49 |
|
| 50 |
``` |
| 51 |
> npm install -g find-requires |
| 52 |
``` |
| 53 |
|
| 54 |
Find all requires in a file: |
| 55 |
|
| 56 |
``` |
| 57 |
> find-requires file1.js |
| 58 |
test1.js:3:LIB + '/test2' |
| 59 |
test1.js:4:fs |
| 60 |
``` |
| 61 |
|
| 62 |
Find all places the fs module is required: `find-requires -m fs $(find . -name '*.js')` |
| 63 |
|
| 64 |
## Tests |
| 65 |
|
| 66 |
$ npm test |
| 67 |
|
| 68 |
[nix-build-image]: https://semaphoreci.com/api/v1/medikoo-org/find-requires/branches/master/shields_badge.svg |
| 69 |
[nix-build-url]: https://semaphoreci.com/medikoo-org/find-requires |
| 70 |
[win-build-image]: https://ci.appveyor.com/api/projects/status/7i22v2m9om08fklw?svg=true |
| 71 |
[win-build-url]: https://ci.appveyor.com/project/medikoo/find-requires |
| 72 |
[transpilation-image]: https://img.shields.io/badge/transpilation-free-brightgreen.svg |
| 73 |
[npm-image]: https://img.shields.io/npm/v/find-requires.svg |
| 74 |
[npm-url]: https://www.npmjs.com/package/find-requires |