Skip to content

Commit d124c08

Browse files
authored
docs: Document npm_old_version and npm_new_version environment variables (#9385)
npm sets two additional environment variables `npm_old_version` and `npm_new_version` when running the `preversion`, `version`, `postversion` scripts, but these aren’t documented anywhere. Document the variables in the ‘Scripts’ docs, and cross-reference them from the documentation for the version command (and the libnpmversion readme). I've tried to match the existing formatting conventions for each document. Please let me know if anything needs to change.
1 parent 4296f64 commit d124c08

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

docs/lib/content/commands/npm-version.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ The exact order of execution is as follows:
6969
6. Run the `postversion` script.
7070
Use it to clean up the file system or automatically push the commit and/or tag.
7171

72+
For the `preversion`, `version` and `postversion` scripts, npm also sets the [environment variables](/using-npm/scripts#environment) `npm_old_version` and `npm_new_version`.
73+
7274
Take the following example:
7375

7476
```json

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ For example, if you had `{"name":"foo", "version":"1.2.5"}` in your package.json
290290

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

293+
#### versioning variables
294+
295+
For versioning scripts (`preversion`, `version`, `postversion`), npm sets these environment variables:
296+
297+
* `npm_old_version` - The version before being bumped
298+
* `npm_new_version` – The version after being bumped
299+
293300
#### current lifecycle event
294301

295302
Lastly, the `npm_lifecycle_event` environment variable is set to whichever stage of the cycle is being executed.

workspaces/libnpmversion/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ The exact order of execution is as follows:
8585
6. Run the `postversion` script. Use it to clean up the file system or
8686
automatically push the commit and/or tag.
8787

88+
For the `preversion`, `version` and `postversion` scripts, npm also sets the
89+
environment variables `npm_old_version` and `npm_new_version`.
90+
8891
Take the following example:
8992

9093
```json

0 commit comments

Comments
 (0)