首页 > 解决方案 > 如何读取具有 node_modules 平面依赖关系的 npm 项目的源代码?

问题描述

在 npm 将其依赖文件结构从 更改为 之前nestedflat我可以轻松地通过依赖树向下钻取项目依赖项的源代码和依赖项的依赖关系,从而对项目有深入的了解。

然而,现在项目flatly的根目录node_modules中有数千个依赖项,因此无法理解和浏览项目真正的依赖项结构。

如果您想深入了解开源项目,这些天您如何浏览项目依赖关系树?

标签: javascriptnode.jsnpm

解决方案


使用 NPM 命令行,您可以使用npm ls它将输出依赖关系图树。

Yarn 命令行对这种调查有一个很好的功能:yarn why

请参阅此处的文档:https ://yarnpkg.com/lang/en/docs/cli/why/

这是一个示例输出:

yarn why doctrine
yarn why v1.7.0
[1/4] Why do we have the module "doctrine"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "doctrine@2.1.0"
info Has been hoisted to "doctrine"
info Reasons this module exists
   - Hoisted from "eslint-plugin-react#doctrine"
   - Hoisted from "eslint#doctrine"
   - Hoisted from "@storybook#react#babel-plugin-react-docgen#react-docgen#doctrine"
=> Found "eslint-plugin-import#doctrine@1.5.0"
info This module exists because "eslint-plugin-import" depends on it.
Done in 0.91s.

推荐阅读