UNPKG

458 BJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3
4const cache = {}
5
6module.exports = function(packageName) {
7 if (packageName in cache) {
8 return cache[packageName]
9 }
10
11 try {
12 const packageJson = JSON.parse(
13 fs.readFileSync(path.resolve('node_modules', packageName, 'package.json'))
14 )
15 cache[packageName] = packageJson.version
16 return packageJson.version
17 } catch (error) {
18 cache[packageName] = null
19 return null
20 }
21}