Skip to content

Commit 2953983

Browse files
authoredJun 22, 2022
fix(view): error on missing version (#5035)
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
1 parent e03009f commit 2953983

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
 

‎lib/commands/view.js

+9
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ class View extends BaseCommand {
236236
}
237237
})
238238

239+
// No data has been pushed because no data is matching the specified version
240+
if (data.length === 0 && version !== 'latest') {
241+
const er = new Error(`No match found for version ${version}`)
242+
er.statusCode = 404
243+
er.code = 'E404'
244+
er.pkgid = `${pckmnt._id}@${version}`
245+
throw er
246+
}
247+
239248
if (
240249
!this.npm.config.get('json') &&
241250
args.length === 1 &&

‎test/lib/commands/view.js

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const packument = (nv, opts) => {
3333
},
3434
},
3535
blue: {
36+
_id: 'blue',
3637
name: 'blue',
3738
'dist-tags': {
3839
latest: '1.0.0',
@@ -464,6 +465,14 @@ t.test('throws when unpublished', async t => {
464465
)
465466
})
466467

468+
t.test('throws when version not matched', async t => {
469+
const { npm } = await loadMockNpm(t)
470+
await t.rejects(
471+
npm.exec('view', ['blue@2.0.0']),
472+
{ code: 'E404', pkgid: 'blue@2.0.0', message: 'No match found for version 2.0.0' }
473+
)
474+
})
475+
467476
t.test('workspaces', async t => {
468477
const prefixDir = {
469478
'package.json': JSON.stringify({

0 commit comments

Comments
 (0)
Please sign in to comment.