Skip to content

Commit 33a50d7

Browse files
Max Blackowlstronaut
authored andcommitted
docs(scripts): update npm_package_* environment variables documentation
1 parent 50508f9 commit 33a50d7

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

docs/lib/content/using-npm/scripts.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,27 @@ then you could run `npm start` to execute the `bar` script, which is exported in
270270

271271
#### package.json vars
272272

273-
The package.json fields are tacked onto the `npm_package_` prefix.
274-
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 `npm_package_version` set to "1.2.5". You can access these variables in your code with `process.env.npm_package_name` and `process.env.npm_package_version`, and so on for other fields.
273+
npm sets the following environment variables from the package.json:
274+
275+
* `npm_package_name` - The package name
276+
* `npm_package_version` - The package version
277+
* `npm_package_bin_*` - Each executable defined in the bin field
278+
* `npm_package_engines_*` - Each engine defined in the engines field
279+
* `npm_package_config_*` - Each config value defined in the config field
280+
* `npm_package_json` - The full path to the package.json file
281+
282+
Additionally, for install scripts (`preinstall`, `install`, `postinstall`), npm sets these environment variables:
283+
284+
* `npm_package_resolved` - The resolved URL for the package
285+
* `npm_package_integrity` - The integrity hash for the package
286+
* `npm_package_optional` - Set to `"true"` if the package is optional
287+
* `npm_package_dev` - Set to `"true"` if the package is a dev dependency
288+
* `npm_package_peer` - Set to `"true"` if the package is a peer dependency
289+
* `npm_package_dev_optional` - Set to `"true"` if the package is both dev and optional
290+
291+
For example, 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 `npm_package_version` set to "1.2.5". You can access these variables in your code with `process.env.npm_package_name` and `process.env.npm_package_version`.
292+
293+
**Note:** In npm 7 and later, most package.json fields are no longer provided as environment variables. Scripts that need access to other package.json fields should read the package.json file directly. The `npm_package_json` environment variable provides the path to the file for this purpose.
275294

276295
See [`package.json`](/configuring-npm/package-json) for more on package configs.
277296

0 commit comments

Comments
 (0)