Socket
Socket
Sign inDemoInstall

detective-cjs

Package Overview
Dependencies
3
Maintainers
3
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    detective-cjs

Get the dependencies of a CommonJS module by traversing its AST


Version published
Maintainers
3
Install size
4.17 MB
Created

Package description

What is detective-cjs?

The detective-cjs package is a tool designed for analyzing JavaScript files to extract their CommonJS (require) dependencies. It parses the source code of a JavaScript file and identifies the modules that the file depends on by looking for 'require' statements. This can be particularly useful for tasks such as bundling, dependency analysis, and refactoring.

What are detective-cjs's main functionalities?

Extracting dependencies from a file

This feature allows you to pass JavaScript source code as a string to the detective function, which returns an array of strings representing the dependencies found in the source code. It's useful for identifying which modules a particular file is dependent on.

const detective = require('detective-cjs');
const src = `const foo = require('foo');
const bar = require('./bar.js');`;
const dependencies = detective(src);
console.log(dependencies); // ['foo', './bar.js']

Other packages similar to detective-cjs

Readme

Source

detective-cjs

CI npm version npm downloads

Get the dependencies of a CommonJS module by traversing its AST

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

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);
  • Supports JSX, ES7, and any other features that node-source-walk supports.

License

MIT

Keywords

FAQs

Last updated on 05 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc