Skip to content

[BUG] npm run <script name> --if-present silently ignores missing scripts in subcommands #3352

@papandreou

Description

@papandreou

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

The --if-present switch that npm run supports also cascades into other npm run commands executed by the script at all levels. It makes the --if-present switch a bit of a footgun, so I hope that's a bug :)

Background: I spent some time figuring out why a missing script referenced by an npm run ... inside another npm script didn't cause my Heroku deploy to fail. It turns out that it's because they use the --if-present flag when executing the top-level build script.

The problem can be illustrated with a simple package.json:

{
  "scripts": {
    "foo": "npm run notfound"
  }
}

To reproduce:

npm run --if-present foo

This command exists with 0 and no error message.

Expected Behavior

I expected it to fail with missing script: notfound.

I would expect npm run --if-present <script name> to only apply its "silently ignore missing script" behavior wrt. the exact script I tell it to run. I don't want the behavior to cascade into other npm run ... commands that might be found in the script itself. If I wanted that, I would pass --if-present there explicitly also.

Steps To Reproduce

See "Current Behavior"

Environment

  • OS: OSX 10.15.7 (19H1217)
  • Node: 16.2.0
  • npm: 7.15.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingPriority 2secondary priority issueRelease 7.xwork is associated with a specific npm 7 releaseRelease 8.xwork is associated with a specific npm 8 releasecmd:run-scriptrelated to `npm run-script`

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions