[英]Get the version number of an installed Node package without loading it?
It is possible to get the version of the current package via 可以通过获取当前软件包的版本
const { version } = require('./package.json')
but how can I get the version number of an arbitrary, installed package without loading it? 但是如何在不加载的情况下获取任意已安装软件包的版本号?
I found a solution with require.resolve
: 我找到了一个require.resolve
的解决方案:
const path = require('path')
// get version number without requiring the module
function packageVersion(moduleName) {
const dir = path.dirname(require.resolve(moduleName))
return require(path.join(dir, 'package.json')).version
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.