Skip to content

[BUG] npm view --json outputs illegal JSON for existing package but non-existent version #3184

Closed

Description

@rjgotten

Current Behavior:

npm view <package>@<version> --json returns an empty or white-space string when answering a query for a package which does exist, but a version descriptor which has no matches. This is illegal JSON and attempting to JSON.parse it throws an error.

Expected Behavior:

npm view <package>@<version> --json should return valid JSON in this case: either null or something that is contextually relevant to the kind of query, such as perhaps an empty array []. E.g.

> npm view npm@10 --json
null

> npm view npm@10 version --json
[]

Steps To Reproduce:

Execute npm view npm@10 --json.

Environment:

OS: any
Node: any
npm: 6+

Activity

  1. added
    Bugthing that needs fixing
    Needs Triageneeds review for next steps
    Release 7.xwork is associated with a specific npm 7 release
    on May 4, 2021
  2. added
    Priority 2secondary priority issue
    and removed
    Needs Triageneeds review for next steps
    on May 12, 2021
  3. varungandhi-src commented on Nov 8, 2021

    @varungandhi-src

    It looks like this problem isn't specific to the --json flag. Without the --json flag, as of npm 7.20.3, using npm view react@9999 will exit successfully instead of emitting an error and exiting with a non-zero exit code.

  4. self-assigned this
    on May 11, 2024
  5. lukekarrys commented on May 13, 2024

    @lukekarrys
    Contributor

    All of these cases now correctly set the exit code to 1 and show the relevant error. In --json mode that error is present in the json output.

    ❯ npm --version
    10.7.0
    
    ❯ npm view npm@11 --json
    npm error code E404
    npm error 404 No match found for version 11
    npm error 404
    npm error 404  'npm@11' is not in this registry.
    npm error 404
    npm error 404 Note that you can also install from a
    npm error 404 tarball, folder, http url, or git url.
    {
      "error": {
        "code": "E404",
        "summary": "No match found for version 11",
        "detail": "\n 'npm@11' is not in this registry.\n\nNote that you can also install from a\ntarball, folder, http url, or git url."
      }
    }
    
    npm error A complete log of this run can be found in: /Users/lukekarrys/.npm/_logs/2024-05-13T22_12_44_503Z-debug-0.log
    
    ❯ npm view npm@11 version --json
    npm error code E404
    npm error 404 No match found for version 11
    npm error 404
    npm error 404  'npm@11' is not in this registry.
    npm error 404
    npm error 404 Note that you can also install from a
    npm error 404 tarball, folder, http url, or git url.
    {
      "error": {
        "code": "E404",
        "summary": "No match found for version 11",
        "detail": "\n 'npm@11' is not in this registry.\n\nNote that you can also install from a\ntarball, folder, http url, or git url."
      }
    }
    
    npm error A complete log of this run can be found in: /Users/lukekarrys/.npm/_logs/2024-05-13T22_13_04_350Z-debug-0.log
    
    ❯ npm view npm@11
    npm error code E404
    npm error 404 No match found for version 11
    npm error 404
    npm error 404  'npm@11' is not in this registry.
    npm error 404
    npm error 404 Note that you can also install from a
    npm error 404 tarball, folder, http url, or git url.
    
    npm error A complete log of this run can be found in: /Users/lukekarrys/.npm/_logs/2024-05-13T22_13_19_311Z-debug-0.log
    
    ❯ npm view npm@11 version
    npm error code E404
    npm error 404 No match found for version 11
    npm error 404
    npm error 404  'npm@11' is not in this registry.
    npm error 404
    npm error 404 Note that you can also install from a
    npm error 404 tarball, folder, http url, or git url.
    
    npm error A complete log of this run can be found in: /Users/lukekarrys/.npm/_logs/2024-05-13T22_13_27_269Z-debug-0.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Bugthing that needs fixingPriority 2secondary priority issueRelease 7.xwork is associated with a specific npm 7 release

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions