Skip to content

[BUG] --json outputs errors to stdout instead of stderr #2150

Description

@raineorshine

Current Behavior:

Running npm ls --json with missing dependencies outputs the error to stdout instead of stderr. This creates two top-level JSON objects, making it unparseable.

This is a regression from v6.

May be related to: #1109

Expected Behavior:

Normal output should print to stdout, errors should print to stderr.

i.e.

stdout:

{
  "name": "temp",
  "problems": [
    "missing: foo@1.0.0, required by temp@"
  ],
  "dependencies": {
    "foo": {
      "required": "1.0.0",
      "missing": true,
      "problems": [
        "missing: foo@1.0.0, required by temp@"
      ]
    }
  }
}

stderr:

{
  "error": {
    "code": "ELSPROBLEMS",
    "summary": "missing: foo@1.0.0, required by temp@",
    "detail": ""
  }
}

Steps To Reproduce:

For the life of me I can't figure out why this test passes, as it clearly asserts the expected stdout.

That said, I can reproduce the issue running the cli manually on a simple package.json:

$ mkdir issue-2150 && cd issue-2150
$ echo '{ "dependencies": { "foo": "1.0.0" } }' > package.json
$ npm ls --json 2>/dev/null

2>/dev/null makes it clear that the following output is all on stdout:

{
  "name": "temp",
  "problems": [
    "missing: foo@1.0.0, required by temp@"
  ],
  "dependencies": {
    "foo": {
      "required": "1.0.0",
      "missing": true,
      "problems": [
        "missing: foo@1.0.0, required by temp@"
      ]
    }
  }
}
{
  "error": {
    "code": "ELSPROBLEMS",
    "summary": "missing: foo@1.0.0, required by temp@",
    "detail": ""
  }
}

Environment:

OS: 10.14.6
Node: 15.1.0
npm: 7.0.9

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingNeeds Triageneeds review for next stepsRelease 7.xwork is associated with a specific npm 7 release

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions