detective-typescript
May 19, 2026 ยท View on GitHub
Get the dependencies of TypeScript module
npm install detective-typescript typescript
Usage
ESM
import fs from 'node:fs';
import detective from 'detective-typescript';
const mySourceCode = fs.readFileSync('myfile.ts', 'utf8');
// Pass in a file's content or an AST
const dependencies = detective(mySourceCode);
// For TSX/JSX files
const tsxDependencies = detective.tsx(mySourceCode);
CommonJS
const { default: detective } = require('detective-typescript');
Options
| Option | Type | Default | Description |
|---|---|---|---|
skipTypeImports | boolean | false | Skip imports that only import types |
mixedImports | boolean | false | Include CJS require() calls in the dependency list |
skipAsyncImports | boolean | false | Omit dynamic import('foo') expressions |
jsx | boolean | false | Enable parsing of JSX/TSX syntax |
onFile | Function | - | Callback invoked before a file is processed. Receives { options, src, ast, walker }. Intended for use with dependency-tree and precinct. |
onAfterFile | Function | - | Like onFile, but also receives dependencies (string array of extracted dependencies). |