Skip to content

[RRFC] "npm pkg get <value> --raw" to get raw unquoted values for further use in scripts #634

Closed

Description

@ThePlenkov

Current behavior

One of the most popular use cases for pkg get command is getting values for other scripts as variables. However currently it's returning the value with quotes:

npm pkg get version 
"4.12.2"

The current solution is to use it in a pipe and remove quotes somehow with sed or jq.:
Example:

nx build app version=$(npm pkg get version | jq -r)

However it comes with some cons:

  • It's not cross-platform
  • Requires additional packages installation
  • Code looks more complicated
  • No unique de-facto standard

Desired Behaviour

To introduce --raw/-r flag for npm pkg get command
Thus, for the following command we will expect the raw value:

npm pkg get version --raw
4.12.2

So the previous example would look like :

nx build app version=$(npm pkg get version --raw)

References

It's a simple case but you can see how much discussion about this topic:
https://gist.github.com/DarrenN/8c6a5b969481725a4413
https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code

Activity

  1. ljharb commented on Sep 15, 2022

    @ljharb
    Contributor

    This feature makes sense to me.

  2. ruyadorno commented on Sep 15, 2022

    @ruyadorno
    Contributor

    This sounds like a good idea! I just believe that what we want here is to add support to --parseable instead of adding a new raw config in the mix.

    Just to add some background, there are basically 3 different types of output that the npm cli supports:

    • human readable (most often the default, prints things like trees, uses colors, etc)
    • json (I believe this is the default for npm pkg get today, prints stringified json output)
    • parseable (a more cli-friendly output, separates things with either tabs or colons, supported in npm ls, npm outdated, npm search and more)

    That said, maybe this is too small of a feature to afford a full RFC. I can see this being an realatively-easy-to-merge contribution in case someone from the community submits a PR, or else it should be a backlogged item for the npm cli team.

  3. ljharb commented on Sep 15, 2022

    @ljharb
    Contributor

    parseable and a PR sounds great to me :-) hopefully the npm cli team can weigh in.

  4. bnb commented on Sep 21, 2022

    @bnb

    very much +1 to parsable

  5. darcyclarke commented on Sep 28, 2022

    @darcyclarke
    Contributor

    Looks good. I'm +1 to npm version --parseable - if someone wants to pick this up & make a PR happen that would be much appreciated (don't think we need a full-blown RFC/conversation about this, as @ruyadorno noted)

  6. added
    Agenda
    on Sep 28, 2022
  7. lukekarrys commented on Nov 10, 2022

    @lukekarrys
    Contributor

    Tracking as a feature enhancement in the CLI here: npm/cli#5508

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

    Agenda

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions