You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/lib/content/using-npm/scripts.md
+21-2Lines changed: 21 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -270,8 +270,27 @@ then you could run `npm start` to execute the `bar` script, which is exported in
270
270
271
271
#### package.json vars
272
272
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.
275
294
276
295
See [`package.json`](/configuring-npm/package-json) for more on package configs.
0 commit comments