https://github.com/dependents/node-detective-cjs
Get the dependencies of a CommonJS module by traversing its AST
https://github.com/dependents/node-detective-cjs
commonjs detective
Last synced: 2 months ago
JSON representation
Get the dependencies of a CommonJS module by traversing its AST
- Host: GitHub
- URL: https://github.com/dependents/node-detective-cjs
- Owner: dependents
- License: mit
- Created: 2014-12-14T02:49:41.000Z (over 11 years ago)
- Default Branch: main
- Last Pushed: 2026-04-03T18:35:20.000Z (3 months ago)
- Last Synced: 2026-04-06T20:32:48.698Z (2 months ago)
- Topics: commonjs, detective
- Language: JavaScript
- Homepage:
- Size: 514 KB
- Stars: 8
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# detective-cjs
[](https://github.com/dependents/node-detective-cjs/actions/workflows/ci.yml?query=branch%3Amain)
[](https://www.npmjs.com/package/detective-cjs)
[](https://www.npmjs.com/package/detective-cjs)
> Get the dependencies of a CommonJS module by traversing its AST
```sh
npm install detective-cjs
```
But dude, substack already built this: node-detective. Yes, but I needed the capability to reuse an AST
and this was unlikely to be merged timely. I can also support jsx and other syntactic constructs faster.
## Usage
```js
const fs = require('fs');
const detective = require('detective-cjs');
const mySourceCode = fs.readFileSync('myfile.js', 'utf8');
// Pass in a file's content or an AST
const dependencies = detective(mySourceCode);
// Use skipLazyLoaded to only include top-level requires,
// treating inline (lazy-loaded) requires as intentional and legal
const topLevelOnly = detective(mySourceCode, { skipLazyLoaded: true });
```
* Supports JSX, ES7, and any other features that [node-source-walk](https://github.com/dependents/node-source-walk) supports.
## License
[MIT](LICENSE)