0

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?

Jakob
  • 3,570
  • 3
  • 36
  • 49

1 Answers1

0

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
}
Jakob
  • 3,570
  • 3
  • 36
  • 49