Skip to content

[BUG] npm pkg get version returns the correct version in inconsistent and broken formats #8189

@vemonet

Description

@vemonet

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

When running npm pkg get version I get the version of the package returns in weird format, inconsistent, which makes it impossible to use in practice.

You can reproduce it easily here: https://github.com/sib-swiss/sparql-editor it is a monorepo with 2 packages in the packages folder

Running npm pkg get version in the root folder returns:

"0.0.0"

Running the same in the packages/sparql-editor folder returns:

{
  "@sib-swiss/sparql-editor": "0.2.5"
}

The version numbers are right, and I noticed they are just directly extracted from the version field of the local package.json. Which makes sense.

But when used in a sub folder of the workspace then npm will return a single entry JSON with the pkg name as key... It is really hard to understand why.

There are no confusion possible because there can be only one package.json in the folder, so adding this key is useless

Plus if I wanted to parse JSON I would just have parsed the package.json directly. The whole point of npm pkg get is to get the value of a key from the package.json

Expected Behavior

When running npm pkg get <key> I should get just the key value for the package of the current folder:

"0.0.0"

To be honest would even prefer without quotes, but I guess this is to keep track of the data type (in case it is an integer)

Steps To Reproduce

git clone https://github.com/sib-swiss/sparql-editor
cd sparql-editor
npm pkg get version

cd packages/sparql-editor
npm pkg get version

Code for npm pkg: https://github.com/npm/cli/blob/latest/lib/commands/pkg.js

Environment

  • npm: 11.2.0
  • Node.js: v23.9.0
  • OS Name: macOS Sonoma 14.4.1 x86_64
  • System Model Name: Macbook Pro
  • npm config:
; "user" config from /Users/vemonet/.npmrc

//registry.npmjs.org/:_authToken = (protected)

; node bin location = /Users/vemonet/.nvm/versions/node/v23.9.0/bin/node
; node version = v23.9.0
; npm local prefix = /Users/vemonet/dev/expasy/sparql-editor
; npm version = 11.2.0
; cwd = /Users/vemonet/dev/expasy/sparql-editor
; HOME = /Users/vemonet
; Run `npm config ls -l` to show all defaults.

; "publishConfig" from /Users/vemonet/dev/expasy/sparql-editor/package.json
; This set of config values will be used at publish-time.

access = "public"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingNeeds Triageneeds review for next steps

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions