diff --git a/lib/install/deps.js b/lib/install/deps.js index 3fe370140adc3..ff6731f938895 100644 --- a/lib/install/deps.js +++ b/lib/install/deps.js @@ -318,7 +318,18 @@ function computeVersionSpec (tree, child) { if (semver.valid(version, true) && semver.gte(version, '0.1.0', true) && !npm.config.get('save-exact')) { - rangeDescriptor = npm.config.get('save-prefix') + if (requested.type === 'range') { + // add save-prefix '^' or '~' only when the future candidates satisfies the + // requested range version spec + var futureVersion = semver.maxSatisfying([semver.inc(version, 'major'), + semver.inc(version, 'minor'), semver.inc(version, 'patch')], + npm.config.get('save-prefix') + version) + if (semver.satisfies(futureVersion, requested.fetchSpec)) { + rangeDescriptor = npm.config.get('save-prefix') + } + } else { + rangeDescriptor = npm.config.get('save-prefix') + } } if (requested.type === 'alias') { rangeDescriptor = `npm:${requested.subSpec.name}@${rangeDescriptor}`