Registry / devops / babel-plugin-detective

babel-plugin-detective

JSON →
library2.0.0jsnpmunverified

A Babel plugin (versions 5 & 6) that scans the AST for import statements and require() calls, extracting dependency strings and location data. Works with both ESM imports and CommonJS requires, including dynamic expressions. Metadata is stored on the Babel result and can be retrieved via a convenience method. It provides fine-grained filtering via options like `import`, `export`, `require`, and `generated`. Stable version is 2.0.0, which only supports Babel 5/6 (not Babel 7+). Compatible with Node >=0.10.0.

npm install babel-plugin-detective
INSTALL
IMPORT
SIG · BABEL-PLUGIN-DETEC
B
babel-plugin-detective
devopsjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const detective = require('babel-plugin-detective');
import detective from 'babel-plugin-detective';
This package is CommonJS only; there is no default ESM export. It can be required in an ES module context using createRequire.
metadata
const metadata = detective.metadata(babelResult);
const metadata = babelResult.metadata.detective;
metadata is a static method on the detective function, not on the result object. The result.metadata.requires is available, but the convenience method normalizes it.
options
import babel from 'babel-core'; const result = babel.transformFileSync('file.js', { plugins: [['detective', { import: false }]] });
// Using with Babel 7 won't work; this plugin only supports Babel 5/6.
Options are passed as the second element of the plugin array in Babel 6, or via extra.detective in Babel 5.

Uses babel-plugin-detective to extract all string module names from import and require statements in a code snippet.

const babel = require('babel-core'); const detective = require('babel-plugin-detective'); const code = ` const fs = require('fs'); import path from 'path'; `; const result = babel.transformSync(code, { plugins: [[detective, { import: true, require: true }]] }); const requires = detective.metadata(result); console.log(requires.strings); // ['fs', 'path'] console.log(requires.expressions); // []
Debug
Known issues
breakingThis plugin does not support Babel 7+. Use at least babel-core 5 or 6.
fix
If you need Babel 7, use @babel/traverse with a custom visitor or switch to a different package like detective.
affects: >=2.0.0
deprecatedNode.js >=0.10.0 is extremely outdated. Modern installations may encounter issues with missing modules or security vulnerabilities.
fix
Use Node.js >=12.0.0, but note that the plugin may still work. If it fails, try using @babel/plugin-proposal-detective or a different dependency scanner.
affects: >=0.0.0
gotchaWhen using options.generated: true, you may get duplicate entries if ES2015 imports are present, because imports are also detected separately.
fix
Set import: false and require: true to only capture generated require() calls, or deduplicate manually.
affects: >=2.0.0
gotchaThe metadata method requires the full Babel result object, not just the metadata property. Passing result.metadata will not work.
fix
Always pass the result object returned by babel.transform* (e.g., const meta = detective.metadata(transformResult));
affects: >=0.0.0
gotchaDynamic require expressions are captured only as location data, not strings. The plugin does not evaluate expressions.
fix
Use a static analysis tool like detective (the original) or escodegen to evaluate simple expressions, or accept that dynamic dependencies are not resolved.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-core'
babel-core is a peer dependency that must be installed alongside babel-plugin-detective.
fix
Run: npm install babel-core babel-plugin-detective
TypeError: detective.metadata is not a function
The import style is incorrect; detective is a CommonJS module that exports a function with a property, not an ES module.
fix
Use const detective = require('babel-plugin-detective'); instead of import.
Error: Plugin 0 specified in "plugins" provided an invalid property of type "undefined"
The plugin is being used in Babel 7 which expects @babel/plugin-* format. babel-plugin-detective only works with Babel 5/6.
fix
Upgrade to babel-core version 6.x.x or use a different detective plugin for Babel 7.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
babel-plugin-detective — npm install babel-plugin-detective · libregistry