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 🙏

© 2026 – Pkg Stats / Ryan Hefner

simple-module-graph

v0.3.1

Published

一个简单的 `ModuleGraph` 库,灵感来自 Rollup

Readme

simple-module-graph

一个简单的 ModuleGraph 库,灵感来自 Rollup

Install

npm install simple-module-graph

Usage

import { getModuleGraph } from 'simple-module-graph';

const graph = await getModuleGraph({ files: './src/index.ts' });

for (const [id, mod] of graph.modules) {
  console.log(id);
  console.log('  dependencies:', [...mod.dependencies].map((d) => d.id));
  console.log('  importers:', [...mod.importers].map((d) => d.id));
}

支持多个入口:

const graph = await getModuleGraph({ files: ['./src/index.ts', './src/server.ts'] });

Alias

内置别名 @<cwd>/src~<cwd>/node_modules,可自定义覆盖:

const graph = await getModuleGraph({
  files: './src/index.ts',
  alias: { '@': () => '/project/packages/src' },
});

node_modules

第三方依赖作为叶子节点,仅记录依赖关系不深入解析,ID 以 node_modules/ 为前缀。

API

getModuleGraph(options)

| 参数 | 类型 | 默认值 | 说明 | |------|------|--------|------| | files | string \| string[] | — | 入口文件路径 | | cwd | string | process.cwd() | 项目根目录 | | alias | Record<string, () => string> | 内置 @~ | 自定义别名 |

ModuleGraph

| 属性/方法 | 类型 | 说明 | |-----------|------|------| | modules | Map<string, ModuleNode> | 所有模块(key 为解析后的绝对路径) | | entryPoints | Set<ModuleNode> | 入口模块 | | toJSON() | () => object | 序列化图结构,方便调试输出 | | addModule(id, isEntry?) | (string, boolean?) => Promise<ModuleNode \| null> | 添加模块,递归解析依赖 | | resolveId(source, importer?) | (string, string?) => Promise<string \| null> | 解析导入路径(相对/alias/node_modules) | | resolveDependencies(module) | (ModuleNode) => Promise<void> | 解析模块的所有依赖并建立双向连接 | | resolveAlias(source) | (string) => Promise<string \| null> | 别名前缀匹配解析 | | resolveEntryFile(source) | (string) => Promise<string \| null> | 基于 cwd 解析入口文件 | | tryResolveWithExt(source) | (string) => Promise<string \| null> | 自动补全扩展名解析 | | parseAST(code) | (string) => BabelStyleAST | 解析代码为 Babel AST | | extractImports(ast, code) | (BabelStyleAST, string) => ImportInfo[] | 从 AST 提取导入信息 |

ModuleNode

| 属性 | 类型 | 说明 | |------|------|------| | id | string | 模块唯一标识(文件绝对路径或 node_modules/*) | | rawId | string \| undefined | 原始 id(已废弃,仅供参考) | | code | string \| undefined | 源码内容 | | ast | BabelStyleAST \| undefined | Babel AST | | dependencies | Set<ModuleNode> | 当前模块依赖的模块 | | importers | Set<ModuleNode> | 依赖当前模块的模块 | | importedBindings | Map<string, { id, specifiers, importee }> | 导入的变量详情 | | error | { type, message } \| undefined | 解析错误 | | toJSON() | () => object | 安全序列化(循环引用标记为 _circular: true) |