node.js - Node - 递归搜索依赖关系

标签 node.js node-modules

我想递归搜索并查找我是否正在使用某个 Node 模块。这可能吗?

最佳答案

如果你的意思不是在运行时,你可以这样做:

npm ls <module name to search for>

例如:

$ npm ls async
/Users/justin/code/example
├─┬ aws-cdk@0.19.0
│ └─┬ archiver@2.1.1
│   └── async@2.1.4  deduped
├─┬ mongooster@6.0.2
│ └─┬ mongoose@5.3.5
│   └── async@2.6.1
└─┬ sqs-consumer@3.8.0
  └── async@2.1.4

在运行时您可以使用require.resolve

> require.resolve('async')
'/Users/justin/code/example/node_modules/async/dist/async.js'

或者您可以请求该模块,然后在缓存中查找以获取有关该模块的详细信息,包括其父链。

> require.cache[require.resolve('async')]
Module {
  id: '/Users/justin/code/example/node_modules/async/dist/async.js',
  exports: { ... },
  parent: Module { ... },
  filename: '/Users/justin/code/example/node_modules/async/dist/async.js',
  loaded: true,
  children: [],
  paths: [ ... ]
}

关于node.js - Node - 递归搜索依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53988470/

相关文章:

javascript - Mongoose/Express/Nodejs 试图将变量从服务器传递到 html

javascript - Moment.js : Determine whether or not 24 hours has passed or not

azure - 在 Cosmos Db 中执行包含 Node 模块的 UDF(用户定义函数)

javascript - Sequelize "model.associate"属性不再起作用?

angular - 如何将适用于 Javascript 的 TUI 图像编辑器导入到我的 Angular 应用程序中?

node.js - Node-webkit 多平台 native 模块与 nw-gyp

c++ - 如何从 NanAsyncWorker 返回一个新对象?

node.js - Firebase 使用对象数组推送到数据库

node.js - 从 package-lock.json 创建 package.json

node.js - 数据未插入 mongodb