Skip to content

[Feature] Expose --prefer-direct-exec for npm run and add a production note (tracking; blocked by @npmcli/run-script https://github.com/npm/run-script/issues/237) #8509

Closed

Description

@seungwanHam

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

There is no CLI/config switch to make npm run wait for the actual app process (e.g., node) rather than /bin/sh. In containerized environments (Docker/Kubernetes), this can cause npm to exit when the shell exits on SIGTERM, stopping the container before the Node.js app completes its graceful shutdown (e.g., server.close() / cleanup). Documentation also does not clearly highlight this operational footgun or provide an official opt-in workaround at the CLI level.

Expected Behavior

Provide an opt-in in npm/cli to surface a switch that makes npm run wait for the real target process:

  • CLI flag: npm run --prefer-direct-exec
  • Config (.npmrc): prefer-direct-exec=true
  • (Optional) Env: NPM_PREFER_DIRECT_EXEC=1

When enabled (Unix):

  • Either directly spawn simple single-command scripts without a shell, or
  • Exec-replace the shell so npm’s direct child is the target process.

Default behavior remains unchanged for compatibility. Also, add a short “Production note” in the run-script docs explaining this flag and recommending exec or direct exec in containerized environments.

Steps To Reproduce

  1. In a Linux containerized environment (e.g., node:18-slim) with PID 1 = dumb-init or tini, orchestrated by Kubernetes.
  2. With package.json using "start": "node index.js" (no exec).
  3. Run the pod and terminate it via kubectl delete pod (sends SIGTERM).
  4. Observe: The shell (ash/dash) can exit promptly on SIGTERM; npm exits because its direct child is gone; the container stops before the Node app finishes graceful shutdown.
  5. Workaround today: Change the script to "start": "exec node index.js" so the shell exec-replaces into node. There is no CLI flag to achieve this behavior without editing package.json.

Note: This CLI request is blocked by the implementation proposal in @npmcli/run-script: npm/run-script#237

Environment

  • npm: 10.x
  • Node.js: 18.x (or 20.x)
  • OS Name: Linux container
  • System Model Name: Kubernetes (SIGTERM on pod termination; PID1 = dumb-init or tini)

Activity

  1. added
    Bug
    Needs Triage
    on Aug 12, 2025
  2. milaninfy commented on Aug 15, 2025

    @milaninfy
    Contributor

    @seungwanHam Thanks for taking the time to share your idea! New ideas are always appreciated and are better suited for our RFC repo instead since that's the right place in order to get more attention from the rest of the team and the community.

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

    BugNeeds Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions