Skip to content

Commit 09455b7

Browse files
author
Max Black
committed
docs(scripts): update npm_package_* environment variables documentation
1 parent 3f48487 commit 09455b7

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

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

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

269269
#### package.json vars
270270

271-
The package.json fields are tacked onto the `npm_package_` prefix.
272-
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.
271+
npm sets the following environment variables from the package.json:
272+
273+
* `npm_package_name` - The package name
274+
* `npm_package_version` - The package version
275+
* `npm_package_bin_*` - Each executable defined in the bin field
276+
* `npm_package_engines_*` - Each engine defined in the engines field
277+
* `npm_package_config_*` - Each config value defined in the config field
278+
* `npm_package_json` - The full path to the package.json file
279+
280+
Additionally, for install scripts (`preinstall`, `install`, `postinstall`), npm sets these environment variables:
281+
282+
* `npm_package_resolved` - The resolved URL for the package
283+
* `npm_package_integrity` - The integrity hash for the package
284+
* `npm_package_optional` - Set to `"true"` if the package is optional
285+
* `npm_package_dev` - Set to `"true"` if the package is a dev dependency
286+
* `npm_package_peer` - Set to `"true"` if the package is a peer dependency
287+
* `npm_package_dev_optional` - Set to `"true"` if the package is both dev and optional
288+
289+
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`.
290+
291+
**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.
273292

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

0 commit comments

Comments
 (0)