babel-plugin-detective
raw JSON → 2.0.0 verified Sat Apr 25 auth: no javascript
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.
Common errors
error Error: Cannot find module 'babel-core' ↓
cause babel-core is a peer dependency that must be installed alongside babel-plugin-detective.
fix
Run: npm install babel-core babel-plugin-detective
error TypeError: detective.metadata is not a function ↓
cause 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 Error: Plugin 0 specified in "plugins" provided an invalid property of type "undefined" ↓
cause 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.
Warnings
breaking This 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.
deprecated Node.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.
gotcha When 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.
gotcha The 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));
gotcha Dynamic 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.
Install
npm install babel-plugin-detective yarn add babel-plugin-detective pnpm add babel-plugin-detective Imports
- default wrong
import detective from 'babel-plugin-detective';correctconst detective = require('babel-plugin-detective'); - metadata wrong
const metadata = babelResult.metadata.detective;correctconst metadata = detective.metadata(babelResult); - options wrong
// Using with Babel 7 won't work; this plugin only supports Babel 5/6.correctimport babel from 'babel-core'; const result = babel.transformFileSync('file.js', { plugins: [['detective', { import: false }]] });
Quickstart
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); // []