Skip to content

[Feature] support $npm_package_json #2436

@uhop

Description

@uhop
  • I'd be willing to implement this feature (contributing guide)
  • This feature is important to have in this repository; a contrib plugin wouldn't do

Describe the user story

When a dependency is being processed its dependencies (e.g., utilities) should be able to access the current package.json. In the past, it was done using a messy mapping of all possible package.json properties to $npm_package_custom_var, which was clearly a subpar solution, and polluted the environment variable space.

The NPM team realized it and in NPM v7 they decided to provide three $npm_package_ variables: json (a path), name, and version.

Example:

  • package.json:
    {
      "name": "temp",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "start": "node index.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC"
    }
  • index.js:
    Object.keys(process.env)
      .filter(name => /^npm_/i.test(name))
      .sort()
      .forEach(name => console.log(name, process.env[name]));
  • Output:
    $ npm start
    
    > [email protected] start
    > node index.js
    
    npm_command start
    npm_config_cache /Users/uhop/.npm
    npm_config_globalconfig /Users/uhop/.nvm/versions/node/v15.7.0/etc/npmrc
    npm_config_init.module /Users/uhop/.npm-init.js
    npm_config_init_module /Users/uhop/.npm-init.js
    npm_config_metrics_registry https://registry.npmjs.org/
    npm_config_node_gyp /Users/uhop/.nvm/versions/node/v15.7.0/lib/node_modules/npm/node_modules/node- 
    gyp/bin/node-gyp.js
    npm_config_prefix /Users/uhop/.nvm/versions/node/v15.7.0
    npm_config_user_agent npm/7.4.3 node/v15.7.0 darwin x64
    npm_config_userconfig /Users/uhop/.npmrc
    npm_execpath /Users/uhop/.nvm/versions/node/v15.7.0/lib/node_modules/npm/bin/npm-cli.js
    npm_lifecycle_event start
    npm_lifecycle_script node index.js
    npm_node_execpath /Users/uhop/.nvm/versions/node/v15.7.0/bin/node
    npm_package_json /Users/uhop/Work/temp/package.json
    npm_package_name temp
    npm_package_version 1.0.0

You can see the last three are a new way to deal with package.json — a name, a version, and, for everything else there is a path, which can be used to process package.json any way you like it.

Describe the solution you'd like

We already have npm_package_name and npm_package_version. If we add npm_package_json, we will complete it. It provides a way for "everything else" and adds compatibility.

Describe the drawbacks of your solution

This is a low-risk solution already used by NPM v7. It has virtually no drawbacks and is trivial to implement.

Describe alternatives you've considered

Other ideas, which involved providing solutions outside of yarn or npm proved to be fruitless and unreliable. See this thread as an example: uhop/install-artifact-from-github#1 (comment)

One frequently proposed "solution" involves $PWD, which is a Bash variable. Unfortunately, the majority of users still use Windows — obviously, it depends on the package. Another thing pointed out by users is that $PWD being a Bash variable is set for a current directory, which may not coincide with a processed package.json file for multiple reasons (batch files, specific build environments, dependency chains, and so on).

In any case, implementing the environment variable is an easy win with no discernable drawbacks and uncertainty: a compatible and documented way to deal with the currently processed package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions