{"id":10738,"library":"detective-typescript","title":"TypeScript Dependency Detective","description":"detective-typescript is a utility library designed to extract the static dependencies from TypeScript module source code or its Abstract Syntax Tree (AST). It provides a programmatic API to analyze imports and exports, including support for various TypeScript features like type-only imports, dynamic imports, and JSX syntax. The current stable version is 14.1.1, with releases occurring somewhat regularly to support newer TypeScript versions and Node.js environments, and to address bug fixes and performance improvements. It differentiates itself by offering fine-grained control over dependency extraction through options like `skipTypeImports`, `mixedImports`, and `skipAsyncImports`, making it suitable for tools that need to build dependency graphs or perform static analysis on TypeScript projects. Its dependency extraction mechanism is robust, relying on AST parsing provided by `@typescript-eslint/typescript-estree`, and it is often used by build tools, bundlers, and linters for static code analysis.","status":"active","version":"14.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/dependents/detective-typescript","tags":["javascript","detective","typescript","dependencies","module","ast","import"],"install":[{"cmd":"npm install detective-typescript","lang":"bash","label":"npm"},{"cmd":"yarn add detective-typescript","lang":"bash","label":"yarn"},{"cmd":"pnpm add detective-typescript","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for parsing TypeScript syntax; moved to peer dependency in v13.0.0.","package":"typescript","optional":false}],"imports":[{"note":"The main export is a default function, not a named export.","wrong":"import { detective } from 'detective-typescript';","symbol":"detective","correct":"import detective from 'detective-typescript';"},{"note":"CommonJS syntax shown in older documentation, still supported for Node.js environments.","wrong":"import detective from 'detective-typescript';","symbol":"detective","correct":"const detective = require('detective-typescript');"}],"quickstart":{"code":"import fs from 'node:fs';\nimport path from 'node:path';\nimport detective from 'detective-typescript';\n\n// Example TypeScript code with various import types\nconst tsSourceCode = `\nimport { SomeType } from './types';\nimport { utilityFunction } from '@scope/utils';\nimport defaultExport from 'my-module';\nimport * as all from './all-exports';\nconst dynamicImport = import('./lazy-module');\n\ninterface MyInterface extends SomeType {}\n\nexport class MyClass {\n  constructor() {\n    utilityFunction();\n    console.log(defaultExport);\n  }\n}\n`;\n\n// Create a dummy file to simulate reading from disk\nconst filePath = path.join(process.cwd(), 'temp-file.ts');\nfs.writeFileSync(filePath, tsSourceCode, 'utf8');\n\ntry {\n  const mySourceCode = fs.readFileSync(filePath, 'utf8');\n\n  // Get all dependencies, including dynamic imports\n  const dependencies = detective(mySourceCode, { skipAsyncImports: false });\n  console.log('All dependencies:', dependencies);\n  // Expected: [ './types', '@scope/utils', 'my-module', './all-exports', './lazy-module' ]\n\n  // Get only static dependencies (excluding dynamic imports)\n  const staticDependencies = detective(mySourceCode, { skipAsyncImports: true });\n  console.log('Static dependencies:', staticDependencies);\n  // Expected: [ './types', '@scope/utils', 'my-module', './all-exports' ]\n\n  // Skip type-only imports (if 'SomeType' was a type-only import)\n  const nonTypeDependencies = detective(mySourceCode, { skipTypeImports: true });\n  console.log('Non-type dependencies:', nonTypeDependencies);\n\n} catch (error) {\n  console.error('Error processing file:', error);\n} finally {\n  fs.unlinkSync(filePath);\n}\n","lang":"typescript","description":"Demonstrates extracting static and dynamic dependencies from TypeScript code using various options."},"warnings":[{"fix":"Run `npm install typescript` or `yarn add typescript` in your project alongside `detective-typescript`.","message":"Starting with v13.0.0, the `typescript` package is no longer a direct dependency but a peer dependency. Users must install `typescript` explicitly in their project.","severity":"breaking","affected_versions":">=13.0.0"},{"fix":"Ensure your Node.js environment is version 18 or newer. Update Node.js if necessary.","message":"Version 12.0.0 dropped support for Node.js versions older than 18. Running the package on unsupported Node.js versions will result in errors.","severity":"breaking","affected_versions":">=12.0.0"},{"fix":"When calling `detective(sourceCode, { mixedImports: true })`, ensure `mixedImports` is set to `true`.","message":"The `mixedImports` option defaults to `false`. This means that CommonJS `require()` calls are not included in the dependency list by default. If you need to detect both ESM `import` and CJS `require` statements, you must explicitly set this option to `true`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to `detective-typescript@14.1.1` or newer to ensure correct handling of these options.","message":"Options such as `skipAsyncImports`, `onFile`, and `onAfterFile` were incorrectly forwarded to internal parser/walker constructors in versions prior to v14.1.1. This could lead to unexpected behavior or options not being applied correctly.","severity":"gotcha","affected_versions":"<14.1.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Install TypeScript in your project: `npm install typescript` or `yarn add typescript`.","cause":"The `typescript` package is a peer dependency and was not installed in the project.","error":"Error: Cannot find module 'typescript'"},{"fix":"Update Node.js to version 18 or newer. Ensure your installed `typescript` version (`npm view detective-typescript peerDependencies.typescript`) matches the supported range and can parse your source code.","cause":"The Node.js version is too old (e.g., <18) or the `typescript` peer dependency installed is incompatible with the TypeScript features used in the source code.","error":"SyntaxError: Unexpected token 'export' (or similar parsing errors for modern TS features)"},{"fix":"Use `import detective from 'detective-typescript';` for ESM or `const detective = require('detective-typescript');` for CommonJS.","cause":"Incorrect import statement. The `detective-typescript` package exports its main function as a default export, not a named export.","error":"TypeError: detective is not a function"}],"ecosystem":"npm"}