From fde2463fdf62618029c40bd7f78f4dcc59abf83c Mon Sep 17 00:00:00 2001 From: Oliver Byford Date: Wed, 20 May 2026 18:31:46 +0100 Subject: [PATCH 1/2] Document `npm version` env vars in scripts docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit npm sets two additional environment variables when running the `preversion`, `version`, `postversion` scripts, but these aren’t documented anywhere. Document the variables in the ‘Scripts’ docs. This feels like the right place for these docs as it’s where the other environment variables are listed. --- docs/lib/content/using-npm/scripts.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/lib/content/using-npm/scripts.md b/docs/lib/content/using-npm/scripts.md index 380c4aa49e36f..b01c0bc7ffd77 100644 --- a/docs/lib/content/using-npm/scripts.md +++ b/docs/lib/content/using-npm/scripts.md @@ -290,6 +290,13 @@ For example, if you had `{"name":"foo", "version":"1.2.5"}` in your package.json See [`package.json`](/configuring-npm/package-json) for more on package configs. +#### versioning variables + +For versioning scripts (`preversion`, `version`, `postversion`), npm sets these environment variables: + +* `npm_old_version` - The version before being bumped +* `npm_new_version` – The version after being bumped + #### current lifecycle event Lastly, the `npm_lifecycle_event` environment variable is set to whichever stage of the cycle is being executed. From fcdb5c90619a8d3985f8bd13361c7e287899226c Mon Sep 17 00:00:00 2001 From: Oliver Byford Date: Wed, 20 May 2026 18:40:15 +0100 Subject: [PATCH 2/2] Add reference to env vars in npm version docs --- docs/lib/content/commands/npm-version.md | 2 ++ workspaces/libnpmversion/README.md | 3 +++ 2 files changed, 5 insertions(+) diff --git a/docs/lib/content/commands/npm-version.md b/docs/lib/content/commands/npm-version.md index 23ce28763f6cd..cca8106a00831 100644 --- a/docs/lib/content/commands/npm-version.md +++ b/docs/lib/content/commands/npm-version.md @@ -69,6 +69,8 @@ The exact order of execution is as follows: 6. Run the `postversion` script. Use it to clean up the file system or automatically push the commit and/or tag. +For the `preversion`, `version` and `postversion` scripts, npm also sets the [environment variables](/using-npm/scripts#environment) `npm_old_version` and `npm_new_version`. + Take the following example: ```json diff --git a/workspaces/libnpmversion/README.md b/workspaces/libnpmversion/README.md index 3a91bb471ba56..8f3f89e453a9b 100644 --- a/workspaces/libnpmversion/README.md +++ b/workspaces/libnpmversion/README.md @@ -85,6 +85,9 @@ The exact order of execution is as follows: 6. Run the `postversion` script. Use it to clean up the file system or automatically push the commit and/or tag. +For the `preversion`, `version` and `postversion` scripts, npm also sets the +environment variables `npm_old_version` and `npm_new_version`. + Take the following example: ```json