{"id":19040,"library":"babel-plugin-detective","title":"babel-plugin-detective","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/avajs/babel-plugin-detective","tags":["javascript","babel-plugin","detective","require","import","dependencies","scan","traverse"],"install":[{"cmd":"npm install babel-plugin-detective","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-detective","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-detective","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS only; there is no default ESM export. It can be required in an ES module context using createRequire.","wrong":"import detective from 'babel-plugin-detective';","symbol":"default","correct":"const detective = require('babel-plugin-detective');"},{"note":"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.","wrong":"const metadata = babelResult.metadata.detective;","symbol":"metadata","correct":"const metadata = detective.metadata(babelResult);"},{"note":"Options are passed as the second element of the plugin array in Babel 6, or via extra.detective in Babel 5.","wrong":"// Using with Babel 7 won't work; this plugin only supports Babel 5/6.","symbol":"options","correct":"import babel from 'babel-core';\nconst result = babel.transformFileSync('file.js', {\n  plugins: [['detective', { import: false }]]\n});"}],"quickstart":{"code":"const babel = require('babel-core');\nconst detective = require('babel-plugin-detective');\n\nconst code = `\nconst fs = require('fs');\nimport path from 'path';\n`;\n\nconst result = babel.transformSync(code, {\n  plugins: [[detective, { import: true, require: true }]]\n});\n\nconst requires = detective.metadata(result);\nconsole.log(requires.strings); // ['fs', 'path']\nconsole.log(requires.expressions); // []","lang":"javascript","description":"Uses babel-plugin-detective to extract all string module names from import and require statements in a code snippet."},"warnings":[{"fix":"If you need Babel 7, use @babel/traverse with a custom visitor or switch to a different package like detective.","message":"This plugin does not support Babel 7+. Use at least babel-core 5 or 6.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"Node.js >=0.10.0 is extremely outdated. Modern installations may encounter issues with missing modules or security vulnerabilities.","severity":"deprecated","affected_versions":">=0.0.0"},{"fix":"Set import: false and require: true to only capture generated require() calls, or deduplicate manually.","message":"When using options.generated: true, you may get duplicate entries if ES2015 imports are present, because imports are also detected separately.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Always pass the result object returned by babel.transform* (e.g., const meta = detective.metadata(transformResult));","message":"The metadata method requires the full Babel result object, not just the metadata property. Passing result.metadata will not work.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use a static analysis tool like detective (the original) or escodegen to evaluate simple expressions, or accept that dynamic dependencies are not resolved.","message":"Dynamic require expressions are captured only as location data, not strings. The plugin does not evaluate expressions.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run: npm install babel-core babel-plugin-detective","cause":"babel-core is a peer dependency that must be installed alongside babel-plugin-detective.","error":"Error: Cannot find module 'babel-core'"},{"fix":"Use const detective = require('babel-plugin-detective'); instead of import.","cause":"The import style is incorrect; detective is a CommonJS module that exports a function with a property, not an ES module.","error":"TypeError: detective.metadata is not a function"},{"fix":"Upgrade to babel-core version 6.x.x or use a different detective plugin for Babel 7.","cause":"The plugin is being used in Babel 7 which expects @babel/plugin-* format. babel-plugin-detective only works with Babel 5/6.","error":"Error: Plugin 0 specified in \"plugins\" provided an invalid property of type \"undefined\""}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}