Skip to content
Find all calls to require() no matter how crazily nested using a proper walk of the AST
JavaScript
Latest commit aa17000 Nov 1, 2015 @zertosh zertosh 4.3.1
Failed to load latest commit information.
bench using acorn, faster from 700ms to 560ms to parse jquery Nov 19, 2014
example use the latest esprima, remove support for (require) Feb 2, 2013
test remove escodegen and just grab the expr from the src Oct 31, 2015
.gitignore using acorn, faster from 700ms to 560ms to parse jquery Nov 19, 2014
.npmignore
.travis.yml Remove add 0.10, 0.12 and iojs to travis Apr 25, 2015
LICENSE license file Mar 1, 2013
index.js skip traversing branches without the "word" Nov 1, 2015
package.json 4.3.1 Nov 1, 2015
readme.markdown Use acorn's packaged traverser Aug 20, 2015

readme.markdown

detective

find all calls to require() by walking the AST

build status

example

strings

strings_src.js:

var a = require('a');
var b = require('b');
var c = require('c');

strings.js:

var detective = require('detective');
var fs = require('fs');

var src = fs.readFileSync(__dirname + '/strings_src.js');
var requires = detective(src);
console.dir(requires);

output:

$ node examples/strings.js
[ 'a', 'b', 'c' ]

methods

var detective = require('detective');

detective(src, opts)

Give some source body src, return an array of all the require() calls with string arguments.

The options parameter opts is passed along to detective.find().

var found = detective.find(src, opts)

Give some source body src, return found with:

  • found.strings - an array of each string found in a require()
  • found.expressions - an array of each stringified expression found in a require() call
  • found.nodes (when opts.nodes === true) - an array of AST nodes for each argument found in a require() call

Optionally:

  • opts.word - specify a different function name instead of "require"
  • opts.nodes - when true, populate found.nodes
  • opts.isRequire(node) - a function returning whether an AST CallExpression node is a require call
  • opts.parse - supply options directly to acorn with some support for esprima-style options range and loc

install

With npm do:

npm install detective

license

MIT

Something went wrong with that request. Please try again.