Skip to content

[BUG] NPM 7.x broke the "--json" CLI parameter #2740

Closed
@octogonz

Description

@octogonz

The problem

NPM's --json option allows scripts to invoke an NPM command and receive machine-readable JSON that can be easily processed. In previous NPM versions, the JSON was written to STDOUT and all other messages were written to STDERR, making it easy for a script to extract the JSON.

With NPM 7.x, everything is mixed together on STDERR. This defeats the point of the --json feature, which was to make parsing easy. Instead, a script has to implement heuristics to guess which STDERR lines are JSON or not. Scripts developed for NPM 6 now fail because they are reading the wrong stream.

Steps To Reproduce:

  1. Correct behavior: With NPM 6.x, invoke this command:

    npm search --json
    

    The JSON output is written to STDOUT:

    {
      "error": {
        "code": null,
        "summary": "search must be called with arguments",
        "detail": ""
      }
    }

    The console messages are written to STDERR:

    npm ERR! search must be called with arguments
    npm ERR! A complete log of this run can be found in:
    
  2. Broken behavior: With NPM 7.5.4 (the latest version), invoke the same command. Both outputs are mixed together on STDERR. Nothing is written to STDOUT.

This regression appears to have been introduced with NPM 7.0.0.

Environment:

  • OS: All OS's
  • Node: 12.17.0
  • npm: 7.5.4

Metadata

Metadata

Assignees

No one assigned

    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