Skip to content

Commit

Permalink
fix: adapt to npm@8, fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
dracupid committed Oct 24, 2021
1 parent dd2b665 commit 8e9aaab
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,24 @@ GLOBAL_NPM_PATH = process.env.GLOBAL_NPM_PATH || path.join(
!hasSymbolLink ? '../node_modules/npm' : '../..'
)

module.exports = (function () {
module.exports = (function requireNPM (npmPath) {
GLOBAL_NPM_PATH = npmPath
try {
var npm = require(GLOBAL_NPM_PATH)
var npm = require(npmPath)
if (npm && Object.keys(npm).length) {
return npm
}
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
if (e.message.indexOf('The programmatic API was removed') >= 0) {
// for npm >= 8. Maybe programmatic api via require('npm') is no longer available
// https://github.com/npm/cli/pull/3762
return requireNPM(path.join(npmPath, 'lib/npm.js'))
} else if (e.code !== 'MODULE_NOT_FOUND') {
throw e
}
}
throwNotFoundError()
})()
})(GLOBAL_NPM_PATH)

module.exports.GLOBAL_NPM_PATH = GLOBAL_NPM_PATH
module.exports.GLOBAL_NPM_BIN = GLOBAL_NPM_BIN

0 comments on commit 8e9aaab

Please sign in to comment.