Redefine your preversion
script as follows:
"scripts": {
"preversion": "echo $npm_package_version"
}
Explanation:
This accesses the package.json vars. The npm_package_version
variable is one of the many environment variables that npm creates.
On *nix platforms npm runs package.json scripts by utilizing sh
by default. Therefore you reference the environment variables inside npm scripts using the $
prefix, i.e. $npm_package_version
.
However, on Windows npm runs scripts by using cmd
by default. In which case you'll need to reference the environment variables inside npm scripts using the %...%
syntax. For example:
"preversion": "echo %npm_package_version%"
If cross-platform support is necessary you can avoid the differences in syntax, i.e. the $
prefix v's %...%
, by utilizing cross-var. In which case you will need to redefine your preversion
script as follows:
"scripts": {
"preversion": "cross-var echo $npm_package_version"
}
Tip: If you cd
to your project directory and run npm run env
it will list all the npm_*
prefixed environment variables that npm creates.