Skip to content

docs: Explain environment variables from package.json, .npmrc and npm #7856

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

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from

Conversation

goastler
Copy link

@goastler goastler commented Oct 18, 2024

Environment variables explanation was incorrect before.

  • fixed capitalisation of headings
  • added the env vars populated from package.json
  • added the env vars populated from .npmrc
  • added the env vars populated from npm

I haven't been able to get any more env vars from the package.json other than the name, version, engines, config and bin. If more env vars are populated, I'll adjust the documentation, but they're the only ones afaict

@goastler goastler requested a review from a team as a code owner October 18, 2024 15:25
Copy link
Contributor

@mbtools mbtools left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this addition. LGTM 👍

The package.json fields are tacked onto the `npm_package_` prefix. So,
for instance, if you had `{"name":"foo", "version":"1.2.5"}` in your
package.json file, then your package scripts would have the
`npm_package_name` environment variable set to "foo", and the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These still exist and are very load bearing

$ npm run env|grep name
npm_package_name=npm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this what the extended example includes (L282)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No the name/version entries are distinct from the config. They are powered by entries in package.json and can't be change via config.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the example matches the code:

https://github.com/npm/run-script/blob/795e49e5571789efc4c4ebf3f5975ec9267bdc6d/lib/package-envs.js#L21-L29

Agree. We should be more explicit. How about:

  • Only name, version, engines, config, and bin from package.json are available. Other fields are not exposed as environment variables (see RFC). If you want to know what is in package.json, you can read and parse the file or use require('./package.json'), for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants