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

@davidwells/extract-deps

v0.1.4

Published

Get imports and requires from a codebase without AST

Downloads

1,043

Readme

extract-deps

A lightweight utility for extracting imports, requires, and exports from JavaScript/TypeScript code without using AST parsing.

Features

  • Extract ES6 imports (static and dynamic)
  • Extract CommonJS requires (static and dynamic)
  • Extract ES6 exports (default, named, namespace, type exports)
  • Parse code without AST for better performance
  • Support for destructuring in imports/requires
  • TypeScript support for type exports and imports
  • Handles complex patterns like re-exports and aliased exports

Installation

npm install @davidwells/extract-deps

Usage

Basic Usage

const { extractDeps } = require('@davidwells/extract-deps')

const code = `
import { test } from 'uvu'
import assert from 'uvu/assert'
const { execSync } = require('child_process')
const fs = require('fs')

export const myFunction = () => {}
export default class MyClass {}
`

const { imports, requires, exports } = extractDeps(code)

console.log('Imports:', imports)
console.log('Requires:', requires)  
console.log('Exports:', exports)

Extract Imports Only

const { getImports, getStaticImports, getDynamicImports } = require('@davidwells/extract-deps')

const code = `
import { test } from 'uvu'
import assert from 'uvu/assert'

// Dynamic imports
const dynamicModule = await import('./utils')
const config = await import('./config.js')
`

// Get all imports
const allImports = getImports(code)

// Get only static imports
const staticImports = getStaticImports(code)

// Get only dynamic imports  
const dynamicImports = getDynamicImports(code)

console.log('All imports:', allImports)
console.log('Static imports:', staticImports)
console.log('Dynamic imports:', dynamicImports)

Extract Requires Only

const { getRequires, getStaticRequires, getDynamicRequires } = require('@davidwells/extract-deps')

const code = `
const { test } = require('uvu')
const assert = require('uvu/assert')
const fs = require('fs')
`

// Get all requires
const allRequires = getRequires(code)

// Get only static requires
const staticRequires = getStaticRequires(code)

// Get only dynamic requires
const dynamicRequires = getDynamicRequires(code)

console.log('All requires:', allRequires)
console.log('Static requires:', staticRequires)
console.log('Dynamic requires:', dynamicRequires)

Extract Exports Only

const { 
  getExports, 
  getDefaultExports, 
  getNamedExports,
  getNamespaceExports,
  getTypeExports 
} = require('@davidwells/extract-deps')

const code = `
// Default exports
export default function myFunction() {}
export default class MyClass {}

// Named exports
export const myVariable = 'value'
export function myFunction() {}

// Named declaration exports
export { myFunction, anotherFunction }
export { myFunction as aliasedFunction }

// Re-exports
export * from './module'
export * as namespace from './module'

// Type exports
export type MyType = string
export interface MyInterface {}
`

// Get all exports
const allExports = getExports(code)

// Get specific export types
const defaultExports = getDefaultExports(code)
const namedExports = getNamedExports(code)
const namespaceExports = getNamespaceExports(code)
const typeExports = getTypeExports(code)

console.log('All exports:', allExports)
console.log('Default exports:', defaultExports)
console.log('Named exports:', namedExports)
console.log('Namespace exports:', namespaceExports)
console.log('Type exports:', typeExports)

API Reference

extractDeps(code)

Main function that extracts all imports, requires, and exports from the given code.

Parameters:

  • code (string): The JavaScript/TypeScript code to analyze

Returns:

  • Object with imports, requires, and exports arrays

Import Functions

  • getImports(code) - Get all imports (static and dynamic)
  • getStaticImports(code) - Get only static imports
  • getDynamicImports(code) - Get only dynamic imports

Require Functions

  • getRequires(code) - Get all requires (static and dynamic)
  • getStaticRequires(code) - Get only static requires
  • getDynamicRequires(code) - Get only dynamic requires

Export Functions

  • getExports(code) - Get all exports
  • getDefaultExports(code) - Get only default exports
  • getNamedExports(code) - Get only named exports
  • getNamedDeclarationExports(code) - Get named declaration exports
  • getAllExports(code) - Get all exports (alias for getExports)
  • getNamespaceExports(code) - Get namespace exports (export * as)
  • getTypeExports(code) - Get TypeScript type exports

Data Structure

Each import/require/export object contains:

{
  moduleSpecifier: string,    // The module being imported/required
  exportType: string,         // Type of export (for exports)
  exportName: string,         // Name of export (for exports)
  definitions: Array,         // Destructuring and variable definitions
  // ... other properties specific to the type
}

Examples

You can find more examples in the examples/ directory and usage examples in _usage.js.

Performance

This library avoids AST parsing for better performance when you only need to extract dependency information. It uses regex-based parsing optimized for common JavaScript/TypeScript patterns.

Alternative Approaches

License

MIT