1

I have a package.json file that looks like this:

{
  "name": "myapp",
  "version": "0.0.1",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "webpack-dev-server --port=4200",
    "build": "webpack",
    "docker-build": "docker build -t myapp .",
    "docker-run": "docker run -d --name myapp -p 9090:9090 myapp",
    "docker-push": "docker build -t myapp . & docker tag myapp myrepo/myapp:{$VERSION} & docker push myrepo/myapp:{$VERSION}"
  },
... 
}

Im trying to get the version number when I run npm run docker-push so it will automatically tag the release with the version number of the package.json. How can I achieve this?

petur
  • 1,366
  • 3
  • 21
  • 42
  • 1
    Does this answer your question? [How can I reference package version in npm script?](https://stackoverflow.com/questions/48609931/how-can-i-reference-package-version-in-npm-script) – vsync Sep 30 '20 at 17:21

1 Answers1

3

You can use the environment variable npm_package_version.

Tip: To see all available environment variables set by npm you can add "printenv | grep npm" as a script:

{
  "name": "myapp",
  "version": "1.0.0",
  "scripts": {
    "start": "node app.js",
    "version": "echo ${npm_package_version}", // return current version
    "vars": "printenv | grep npm" // return all ENV vars with 'npm'
  }
  // ...
}
gnuns
  • 596
  • 5
  • 12