npm / npm/cli

[BUG] Inconsistent values for `npm_package_*` env variable when executing command in workspace since npm v8.8.0

Open
#4,912 0 comments 5 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

Since npm v8.8.0 (also confirmed with v8.10.0), the environment variables available to a command execution in a workspace seem to be inconsistent:

While the npm_package_json variable still points to the package.json of the given workspace, npm_package_name and npm_package_version are now taken from the root project's package.json.

Example:

npm_package_json=/npm-workspace-test/my-module-a/package.json
npm_package_name=npm-workspace-test # root package name
npm_package_version=1.0.0
Expected Behavior

With npm v8.7.0, npm_package_name and npm_package_version where taken from the workspace:

npm_package_json=/npm-workspace-test/my-module-a/package.json
npm_package_name=my-module-a # workspace package name
npm_package_version=0.0.1

This allowed tools to access for example the name of the package they are currently processing. I would not expect the root project's package name here and therefore suspect this to be a bug.

Steps To Reproduce

I created a demo repository for this issue: https://github.com/RandomByte/npm-workspace-test

git clone https://github.com/RandomByte/npm-workspace-test.git
cd npm-workspace-test && npm install

Get all available environment variables for the workspace command and filter for npm_package_*:

npm exec -w my-module-a -c "env" | grep "npm_package_"

Output with npm v8.8.0 and v8.10.0:

npm_package_json=/npm-workspace-test/my-module-a/package.json
npm_package_name=npm-workspace-test # root package name
npm_package_version=1.0.0

Output with npm v8.7.0:

npm_package_json=/my-module-a/package.json
npm_package_name=my-module-a # workspace package name
npm_package_version=0.0.1
Environment
  • npm: 8.10.0
  • Node.js: v17.9.0
  • OS Name: macOS Monterey
  • System Model Name: MacBook Pro

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

Clone the linked npm-workspace-test repository, install dependencies, and run the provided npm exec -w my-module-a command with npm 8.7.0 and 8.8.0 or later. Compare the npm_package_* output for the workspace; done means npm_package_name and npm_package_version consistently describe the selected workspace rather than the root project.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.