Registry / testing / detective-typescript

detective-typescript

JSON →
library14.1.1jsnpmunverified

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.

npm install detective-typescript
INSTALL
IMPORT
SIG · DETECTIVE-TYPESCRI
D
detective-typescript
testingjavascriptv14.1.1
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.

detective
import detective from 'detective-typescript';
import { detective } from 'detective-typescript';
The main export is a default function, not a named export.
detective
const detective = require('detective-typescript');
import detective from 'detective-typescript';
CommonJS syntax shown in older documentation, still supported for Node.js environments.

Demonstrates extracting static and dynamic dependencies from TypeScript code using various options.

import fs from 'node:fs'; import path from 'node:path'; import detective from 'detective-typescript'; // Example TypeScript code with various import types const tsSourceCode = ` import { SomeType } from './types'; import { utilityFunction } from '@scope/utils'; import defaultExport from 'my-module'; import * as all from './all-exports'; const dynamicImport = import('./lazy-module'); interface MyInterface extends SomeType {} export class MyClass { constructor() { utilityFunction(); console.log(defaultExport); } } `; // Create a dummy file to simulate reading from disk const filePath = path.join(process.cwd(), 'temp-file.ts'); fs.writeFileSync(filePath, tsSourceCode, 'utf8'); try { const mySourceCode = fs.readFileSync(filePath, 'utf8'); // Get all dependencies, including dynamic imports const dependencies = detective(mySourceCode, { skipAsyncImports: false }); console.log('All dependencies:', dependencies); // Expected: [ './types', '@scope/utils', 'my-module', './all-exports', './lazy-module' ] // Get only static dependencies (excluding dynamic imports) const staticDependencies = detective(mySourceCode, { skipAsyncImports: true }); console.log('Static dependencies:', staticDependencies); // Expected: [ './types', '@scope/utils', 'my-module', './all-exports' ] // Skip type-only imports (if 'SomeType' was a type-only import) const nonTypeDependencies = detective(mySourceCode, { skipTypeImports: true }); console.log('Non-type dependencies:', nonTypeDependencies); } catch (error) { console.error('Error processing file:', error); } finally { fs.unlinkSync(filePath); }
Debug
Known issues
breakingStarting 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.
fix
Run `npm install typescript` or `yarn add typescript` in your project alongside `detective-typescript`.
affects: >=13.0.0
breakingVersion 12.0.0 dropped support for Node.js versions older than 18. Running the package on unsupported Node.js versions will result in errors.
fix
Ensure your Node.js environment is version 18 or newer. Update Node.js if necessary.
affects: >=12.0.0
gotchaThe `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`.
fix
When calling `detective(sourceCode, { mixedImports: true })`, ensure `mixedImports` is set to `true`.
affects: >=1.0.0
gotchaOptions 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.
fix
Upgrade to `detective-typescript@14.1.1` or newer to ensure correct handling of these options.
affects: <14.1.1
Errors
Common errors & fixes
Error: Cannot find module 'typescript'
The `typescript` package is a peer dependency and was not installed in the project.
fix
Install TypeScript in your project: `npm install typescript` or `yarn add typescript`.
SyntaxError: Unexpected token 'export' (or similar parsing errors for modern TS features)
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.
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.
TypeError: detective is not a function
Incorrect import statement. The `detective-typescript` package exports its main function as a default export, not a named export.
fix
Use `import detective from 'detective-typescript';` for ESM or `const detective = require('detective-typescript');` for CommonJS.
Upgrade
Version history
14.1.1latest on npm
Audit
Dependencies
typescriptrequiredRequired for parsing TypeScript syntax; moved to peer dependency in v13.0.0.
Agent activity
6 hits · last 30 days
node
6
Resources
detective-typescript — npm install detective-typescript · libregistry