Skip to content

[BUG] npm_execpath is set to the path of npx instead of npm #6662

@Zhangdroid

Description

@Zhangdroid

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

The env npm_execpath can be path of npx instead of npm if the user runs a command with npx, e.g. npx node -e "console.log(process.env.npm_execpath)", the output will be /path/to/npm/9.8.1/bin/npx-cli.js

env.npm_execpath = require.main.filename

I did not find the official definition for npm_execpath, but I assume it should only be the path of npm based its name, not the path of npx.

Some popular libraries, e.g npm-run-all, using npm_execpath and assuming it's the package manager path and add run arg to it, so if you run npx npm-run-all test, it is the same as npx run test which will install a package called run and throw errors.

Yarn also suggests use $npm_execpath run <name> which can cause the same problem: https://yarnpkg.com/advanced/rulebook#published-packages-should-avoid-using-npm-run-in-their-scripts

Expected Behavior

npm_execpath should always be set as the path of npm.

Steps To Reproduce

Run npx node -e "console.log(process.env.npm_execpath)"

The output will be /path/to/npm/9.8.1/bin/npx-cli.js

Environment

  • npm: 9.8.1
  • Node.js: 20.4.0
  • OS Name: MacOS
  • System Model Name: Macbook Pro
  • npm config:
n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions