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?
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
:
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
}