npm / npm/cli

[BUG] npx overwrites the `npm_lifecycle_event` with `npx`

Open
#5,784 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Needs Triage Release 8.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

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

npx overwrites the npm_lifecycle_event with npx, and this breaks with past behavior, and breaks use cases like the one below.

Expected Behavior

Given a package.json such as:

{ ...
  "scripts": {
    "data-set-A": "npx data-generator-tool"
 }

Then the process.env.npm_lifecycle_event, as seen by the data-generator-tool, should be "data-set-A"

This was the behavior with npx v6 and before. The use case here is to rely upon the environment variable in order to avoid having to specify an argument to the npx command line tool, especially when that argument will be 100% redundant with the script name.

Others seem to have this same use case: https://stackoverflow.com/questions/69581008/npm-lifecycle-event-different-output-when-updating-the-version-of-npm

Steps To Reproduce

clone:
https://github.com/git-rz/echo-env.git
https://github.com/git-rz/demo-npx-bug.git

Run a local npm registry like verdaccio and publish echo-env to it. Alternatively, install globally and remove the version number from the npx command line in the demo project.
Then npm run test inside of the demo project.

The test will pass when using npm 6 or less, but fail with version>8 .

Environment
  • npm: 8.19.2
  • Node.js: 16.18.0
  • OS Name: darwin
  • System Model Name:
  • npm config:

//host.docker.internal:49160/:_password = (protected)
//host.docker.internal:49160/:username = "me@my.com"
always-auth = true
email = "me@my.com"
; registry = "https://my.com/artifactory/api/npm/npm-dev" ; overridden by env
yes = true

; "env" config from environment

registry = "http://host.docker.internal:49160/"

; node bin location = /Users/me/.config/asdf/installs/nodejs/16.18.0/bin/node
; node version = v16.18.0
; npm local prefix = /Users/me/gh/git-rz/demo-npx-bug
; npm version = 8.19.2
; cwd = /Users/me/gh/git-rz/demo-npx-bug
; HOME = /Users/me
; Run `npm config ls -l` to show all defaults.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked echo-env and demo-npx-bug repositories, then run npm run test in the demo project using the reported npm versions. Compare the npm_lifecycle_event observed by the data generator when invoked through the package script; done means the script name remains data-set-A rather than becoming npx.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.