我想递归搜索并查找我是否正在使用某个 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/