npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

detective-module

v4.1.0

Published

Get the dependencies specifier of an es6 module

Readme

detective-module npm npm

Get the dependencies specifier of an ES6 module and CommonJS require() - Powered by oxc-parser for blazing fast performance

npm install detective-module

✨ Features

  • 🚀 High Performance: Built with oxc-parser (Rust-based) for 3-5x faster parsing
  • ⚡ Modern Testing: Uses Vitest for blazing fast test execution and development experience
  • 📦 Zero Configuration: Works out of the box with intelligent file type detection
  • 🎯 Full Language Support: JavaScript, TypeScript, JSX, TSX, and latest ECMAScript features
  • 🔄 Dual Mode: Supports both ES6 modules (import/export) and CommonJS (require())
  • 🪶 Lightweight: Minimal dependencies with optimized bundle size

Usage

const {
  detectiveModuleAndRequire,
  detectiveModule,
} = require("detective-module");

const mySourceCode = fs.readFileSync("myfile.js", "utf8");

// Pass in a file's content or an AST
const dependencies = detectiveModule(mySourceCode);

// Input:
// import Abc, * as BBBBBB from "mylib";

// Output:
[
  {
    name: "mylib",
    default: "Abc",
    star: true,
    alias: "BBBBBB",
  },
];

Examples

ES6 Modules with Named Imports

// Input:
// import { foo as Foo, bar } from "mylib";

// Output:
[
  {
    name: "mylib",
    members: [
      {
        name: "foo",
        alias: "Foo",
      },
      {
        name: "bar",
        alias: "bar",
      },
    ],
  },
];

TypeScript + React (TSX)

// Input:
// import React, { Component } from 'react';
// import { fetchUser } from '@/services/api';
//
// interface Props {
//   theme: string;
// }
//
// export default class App extends Component<Props> {
//   render() {
//     return <div>Hello World</div>;
//   }
// }

// Output:
[
  {
    name: "react",
    default: "React",
    members: [
      {
        name: "Component",
        alias: "Component",
      },
    ],
  },
  {
    name: "@/services/api",
    members: [
      {
        name: "fetchUser",
        alias: "fetchUser",
      },
    ],
  },
];

CommonJS with Destructuring

// Use detectiveModuleAndRequire for both ES6 and CommonJS
const dependencies = detectiveModuleAndRequire(sourceCode);

// Input:
// const { default: React, useState } = require('react');

// Output:
[
  {
    name: "react",
    default: "React",
    members: [
      {
        name: "useState",
        alias: "useState",
      },
    ],
  },
];

Performance

Thanks to modern tooling (oxc-parser + Vitest), detective-module now offers:

Parsing Performance:

  • ⚡ 3-5x faster than previous Babel-based implementations
  • 🎯 ~45,000+ operations/second on modern hardware
  • 📦 Smaller bundle size with fewer dependencies
  • 🛡️ Better error recovery and parsing reliability

Development Experience:

  • 🧪 2-5x faster test execution with Vitest vs Jest
  • ⚡ Instant hot reload in watch mode (npm run test:watch)
  • 🔥 Native ES modules support without configuration
  • 📊 Better error reporting and debugging experience

API

detectiveModule(code, options?)

Extracts ES6 import/export dependencies from source code.

  • code: Source code string or pre-parsed AST object
  • options.filename: Override file extension for parser context (optional)

detectiveModuleAndRequire(code, options?)

Extracts both ES6 imports/exports and CommonJS require() dependencies.

  • code: Source code string or pre-parsed AST object
  • options.filename: Override file extension for parser context (optional)

Supported Syntax

  • JavaScript (ES5+, ESNext)
  • TypeScript (including latest features)
  • JSX and TSX
  • ES6 Modules (import/export)
  • CommonJS (require()/module.exports)
  • Dynamic imports (import())
  • Stage 3+ proposals and decorators

Migration from v3.x

Version 4.0+ includes major improvements:

  • Parser: Uses oxc-parser instead of node-source-walk for 3-5x better performance
  • Testing: Migrated from Jest to Vitest for faster development and CI/CD
  • API: Remains 100% compatible - no code changes needed for existing users

For Contributors:

  • npm test - Run tests once
  • npm run test:watch - Run tests in watch mode for development

License

MIT