How to display the app version in Angular? How to display the app version in Angular? angular angular

How to display the app version in Angular?


If you want to use/show the version number in your angular app please do the following:

Prerequisites:

  • Angular file and folder structure created via Angular CLI

  • TypeScript 2.9 or later! (Supported from Angular 6.1 upwards)

Steps:

  1. In your /tsconfig.json (sometimes also necessary in /src/tsconfig.app.json) enable the resolveJsonModule option (webpack dev server restart required afterwards):
    "compilerOptions": {      ...      "resolveJsonModule": true      ...
  1. Then in your component, for example /src/app/app.component.ts use the version info:
    import { version } from '../../package.json';    ...    export class AppComponent {      public version: string = version;    }

It's also possible to do step 2 in your environment.ts file, making the version info accessible from there.

Thx @Ionaru and @MarcoRinck for helping out.

This solution will not include the package.json contents, only the version number.
Tested w/Angular8/Node10/TypeScript3.4.3.

Please update your apps to use this solution cause depending on the contents of your package.json the original solution may implicate security issues.


If you're using webpack or angular-cli (which uses webpack), you can just require package.json in your component and display that prop.

const { version: appVersion } = require('../../package.json')// this loads package.json// then you destructure that object and take out the 'version' property from it// and finally with ': appVersion' you rename it to const appVersion

And then you have your component

@Component({  selector: 'stack-overflow',  templateUrl: './stack-overflow.component.html'})export class StackOverflowComponent {  public appVersion  constructor() {    this.appVersion = appVersion  }}


Using the tsconfig option --resolveJsonModule you can import json files in Typescript.

In the environment.ts file:

import { version } from '../../package.json';export const environment = {    VERSION: version,};

You can now use environment.VERSION in your application.