npm version - preversion $npm_package_version


(Luis Lobo Borobia) #1

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 https://docs.npmjs.com/cli/version (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

1.0.6
v1.0.6

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

1.0.6

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

1.0.6

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?


(Kat Marchán) #2

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


(Luis Lobo Borobia) #3

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


(Luis Lobo Borobia) #4

@zkat Just created a new PR: https://github.com/npm/cli/pull/51


(system) #5

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.