Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] $npm_package_dependencies_* are no longer set in v7 #3022

Open
dinvlad opened this issue Apr 3, 2021 · 8 comments
Open

[BUG] $npm_package_dependencies_* are no longer set in v7 #3022

dinvlad opened this issue Apr 3, 2021 · 8 comments
Labels
Bug thing that needs fixing Priority 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release

Comments

@dinvlad
Copy link

dinvlad commented Apr 3, 2021

Current Behavior:

Any $npm_package_dependencies_* variables defined in v6, are no longer set in v7.

Expected Behavior:

$npm_package_dependencies_* variables should list the values from package.json

Steps To Reproduce:

  1. Define a script to list env variables:
  "scripts": {
    "env": "env"
  },
  1. List them:
npm run env | grep -i npm_package | sort
  1. Notice that in v7, several npm_package_* variables are present, but not any of npm_package_dependencies_*
  2. Notice that in v6, all of them are present.

Environment:

OS: Ubuntu 20.10
Node: v15.12.0
npm: 7.8.0

@dinvlad dinvlad added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Apr 3, 2021
@ljharb
Copy link
Collaborator

ljharb commented Apr 3, 2021

This was an intentional change; you’re intended to read the json file in a script instead of relying on the env.

@dinvlad
Copy link
Author

dinvlad commented Apr 4, 2021

@ljharb this is quite inconvenient however - e.g. react-scripts don't enable specifying a dynamic script to read env variables (unlike some others, like vue.config.js). Are there other workarounds for such cases?

Also, the exclusion of _dependencies_* vars doesn't seem to be documented in https://docs.npmjs.com/cli/v7/using-npm/scripts#packagejson-vars - it seems to imply all of them are still present.

@ljharb
Copy link
Collaborator

ljharb commented Apr 4, 2021

Indeed; seems like the docs need updating.

@dinvlad
Copy link
Author

dinvlad commented Apr 4, 2021

Thanks! For the time being, I found a workaround thanks to your suggestion:

"scripts": {
  "start": "npm run react-cli start",
  "build": "npm run react-cli build",
  "react-cli": "node -r ./scripts/react-env.js node_modules/.bin/react-scripts"
}
// ./scripts/react-env.js
const package = require("./package");
process.env.REACT_APP_FIREBASE_VERSION = package.dependencies.firebase;

@nlf nlf added Priority 2 secondary priority issue and removed Needs Triage needs review for next steps labels Apr 6, 2021
@nlf
Copy link
Contributor

nlf commented Apr 6, 2021

keeping open to track updating the documentation to be more clear about this 👍

@superole
Copy link

superole commented Jul 6, 2022

This was an intentional change; you’re intended to read the json file in a script instead of relying on the env.

so when I have a simple npm script that just needs the version string of some dependency (to use as input to a third party script), I am supposed to write a separate nodejs-script to parse package.json (which npm already must have done) and extract that string myself?

That is extremely disappointing ☹️ and poorly documented. I don't even know where to start.

@aljoshare
Copy link

This was an intentional change; you’re intended to read the json file in a script instead of relying on the env.

Can you explain why? It was really helpful and npm_package_name is still working but e.g. npm_package_repository_* is missing as well. It took me some time to find this issue and understand why some fields are working and some not.

@aljoshare
Copy link

At least for my use case npm pkg get PATH_TO_FIELD solved it.

E.g.: npm pkg get repository.url

#3487

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Priority 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

No branches or pull requests

5 participants