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
@@ -268,8 +268,27 @@ then you could run `npm start` to execute the `bar` script, which is exported in
268
268
269
269
#### package.json vars
270
270
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.
273
292
274
293
See [`package.json`](/configuring-npm/package-json) for more on package configs.
0 commit comments