Skip to content

Commit

Permalink
fix(view): error on missing version (#5035)
Browse files Browse the repository at this point in the history
This fixes an error in npm show. When calling npm show with a specific
version of a package that does not exist, it does not show anything and
gives a zero exit code. This has been changed: now it gives a 404 Error
similar to if the package does not exist. Can be tested with npm show
express@5.0.0 (local: node bin/npm-cli.js info express@5.0.0)

Fixes #4964

Co-authored-by: @lukaskuhn-lku
Co-authored-by: @ljharb
  • Loading branch information
wraithgar committed Jun 22, 2022
1 parent e03009f commit 2953983
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/commands/view.js
Expand Up @@ -236,6 +236,15 @@ class View extends BaseCommand {
}
})

// No data has been pushed because no data is matching the specified version
if (data.length === 0 && version !== 'latest') {
const er = new Error(`No match found for version ${version}`)
er.statusCode = 404
er.code = 'E404'
er.pkgid = `${pckmnt._id}@${version}`
throw er
}

if (
!this.npm.config.get('json') &&
args.length === 1 &&
Expand Down
9 changes: 9 additions & 0 deletions test/lib/commands/view.js
Expand Up @@ -33,6 +33,7 @@ const packument = (nv, opts) => {
},
},
blue: {
_id: 'blue',
name: 'blue',
'dist-tags': {
latest: '1.0.0',
Expand Down Expand Up @@ -464,6 +465,14 @@ t.test('throws when unpublished', async t => {
)
})

t.test('throws when version not matched', async t => {
const { npm } = await loadMockNpm(t)
await t.rejects(
npm.exec('view', ['blue@2.0.0']),
{ code: 'E404', pkgid: 'blue@2.0.0', message: 'No match found for version 2.0.0' }
)
})

t.test('workspaces', async t => {
const prefixDir = {
'package.json': JSON.stringify({
Expand Down

0 comments on commit 2953983

Please sign in to comment.