npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

npm version - preversion $npm_package_version

Hi All,

I’m having issues with $npm_package_version in a preversion script. it’s returning the “new to be” version after an npm version patch , for example

let’s say pacakge.json is version 1.0.0.

It is returning the same version in all scripts, preversion, version and postversion

It was working before , like described in (I actually had this working and ironed out)

version | npm Documentation

The place where all things npm are documented

but for some resason… environment? idk yet, it is not working

tired with node 4, 6 ,8, npm versions 3,4,5…

and 6

so: having this package.json file:

  "name": "tmp",
  "version": "1.0.5",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "preversion": "echo $npm_package_version",
    "version": "echo $npm_package_version",
    "postversion": "echo $npm_package_version"
  "description": "",
  "main": "a.js",
  "directories": {
    "test": "test"
  "dependencies": {},
  "devDependencies": {},
  "author": "",
  "license": "ISC"

when I run

npm version patch

i get

$ npm version patch                                                                                                                                                                      [±master ● 59c899c 1.0.5]

> tmp@1.0.6 preversion /home/luis/tmp/2
> echo $npm_package_version


> tmp@1.0.6 version /home/luis/tmp/2
> echo $npm_package_version


> tmp@1.0.6 postversion /home/luis/tmp/2
> echo $npm_package_version


so, super confused…

the docs say:

The exact order of execution is as follows:
1. Check to make sure the git working directory is clean before we get started. Your scripts may add files to the commit in future steps. This step is skipped if the --force flag is set.
2. Run the preversion script. **These scripts have access to the old version in package.json.** A typical use would be running your full test suite before deploying. Any files you want added to the commit should be explicitly added using git add.
3. Bump version in package.json as requested (patch, minor, major, etc).
4. Run the version script. These scripts have access to the new version in package.json (so they can incorporate it into file headers in generated files for example). Again, scripts should explicitly add generated files to the commit using git add.
5. Commit and tag.
6. Run the postversion script. Use it to clean up the file system or automatically push the commit and/or tag.

so, according to 2, preversion should be 1.0.5, right?

I’d take a patch that fixes this :slight_smile: We might not be able to land it until npm@7 though

I’ve fixed it already locally. I’ll submit a PR.

@zkat Just created a new PR: