What is detective?
The 'detective' npm package is a module dependency analysis tool that helps developers to find out which modules are required in a given JavaScript file. It parses the file, extracts the 'require' statements, and returns a list of dependencies. This is particularly useful for tasks like bundling, minification, and understanding code dependencies.
What are detective's main functionalities?
Detect CommonJS dependencies
This feature allows you to extract all CommonJS dependencies (i.e., modules loaded via 'require') from a JavaScript file. The code reads a JavaScript file, uses detective to parse the file and logs the list of dependencies.
const detective = require('detective');
const fs = require('fs');
const content = fs.readFileSync('/path/to/js/file.js', 'utf8');
const dependencies = detective(content);
console.log(dependencies);
Detect ES6 module dependencies
Using 'detective-es6', a variant of detective tailored for ES6 modules, this feature parses files containing ES6 import statements to determine dependencies. The process involves reading the file, parsing it with detective-es6, and logging the dependencies.
const detective = require('detective-es6');
const fs = require('fs');
const content = fs.readFileSync('/path/to/es6/file.js', 'utf8');
const dependencies = detective(content);
console.log(dependencies);
Other packages similar to detective
precinct
Precinct is a tool similar to detective but with broader capabilities. It supports detecting dependencies from multiple module types including CommonJS, AMD, ES6, and TypeScript. Compared to detective, which requires different variants for different module systems, Precinct provides a more unified and versatile approach.
madge
Madge is a more comprehensive tool that builds on the functionality provided by detective. It not only finds dependencies but also creates visual graphs of module dependencies and can detect circular dependencies. This makes Madge suitable for larger projects where understanding the module structure visually can be particularly beneficial.
detective
Find all calls to require() no matter how crazily nested using a proper walk of
the AST.
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()s with string
arguments.
The options parameter opts
is passed along to detective.find()
.
detective.find(src, opts)
Give some source body src
, return an object with "strings" and "expressions"
arrays for each of the require() calls.
The "expressions" array will contain the stringified expressions.
Optionally you can specify a different function besides "require"
to analyze
with opts.word
.
installation
npm install detective