detective-typescript

May 19, 2026 ยท View on GitHub

CI npm version npm downloads

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

OptionTypeDefaultDescription
skipTypeImportsbooleanfalseSkip imports that only import types
mixedImportsbooleanfalseInclude CJS require() calls in the dependency list
skipAsyncImportsbooleanfalseOmit dynamic import('foo') expressions
jsxbooleanfalseEnable parsing of JSX/TSX syntax
onFileFunction-Callback invoked before a file is processed. Receives { options, src, ast, walker }. Intended for use with dependency-tree and precinct.
onAfterFileFunction-Like onFile, but also receives dependencies (string array of extracted dependencies).

License

MIT