Skip to content
This repository was archived by the owner on Feb 19, 2026. It is now read-only.

Commit 8acccac

Browse files
committed
Read env.npm_package_from for npm 7
1 parent a2f6fb4 commit 8acccac

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

bin.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ if (napi.isNapiRuntime(rc.runtime)) napi.logUnsupportedVersion(rc.target, log)
4444

4545
var pm = whichPmRuns()
4646
var isNpm = !pm || pm.name === 'npm'
47+
var origin = util.packageOrigin(process.env, pkg)
4748

4849
if (!isNpm && /node_modules/.test(process.cwd())) {
4950
// From yarn repository
5051
} else if (opts.force) {
5152
log.warn('install', 'prebuilt binaries enforced with --force!')
5253
log.warn('install', 'prebuilt binaries may be out of date!')
53-
} else if (!(typeof pkg._from === 'string')) {
54+
} else if (typeof origin !== 'string') {
5455
log.info('install', 'installing standalone, skipping download.')
5556
process.exit(1)
56-
} else if (pkg._from.length > 4 && pkg._from.substr(0, 4) === 'git+') {
57+
} else if (origin.length > 4 && origin.substr(0, 4) === 'git+') {
5758
log.info('install', 'installing from git repository, skipping download.')
5859
process.exit(1)
5960
} else if (opts.compile === true || opts.prebuild === false) {

util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ function tempFile (cached) {
8787
return cached + '.' + process.pid + '-' + Math.random().toString(16).slice(2) + '.tmp'
8888
}
8989

90+
function packageOrigin (env, pkg) {
91+
if (env.npm_package_from) {
92+
// npm 7: metadata is exposed to environment by arborist
93+
// TODO: seems undefined atm (npm 7.0.2)
94+
return env.npm_package_from
95+
}
96+
97+
if (pkg._from) {
98+
// npm <= 6: metadata is stored on disk in node_modules
99+
return pkg._from
100+
}
101+
}
102+
90103
exports.getDownloadUrl = getDownloadUrl
91104
exports.getApiUrl = getApiUrl
92105
exports.getAssetUrl = getAssetUrl
@@ -95,3 +108,4 @@ exports.cachedPrebuild = cachedPrebuild
95108
exports.prebuildCache = prebuildCache
96109
exports.npmCache = npmCache
97110
exports.tempFile = tempFile
111+
exports.packageOrigin = packageOrigin

0 commit comments

Comments
 (0)