可以通过{p> to get the version of the current package
const { version } = require('./package.json')
但是如何在不加载的情况下获取已安装的任意软件包的版本号?
答案 0 :(得分:0)
我找到了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
}