Home > Net >  Display Pkg Version in Template
Display Pkg Version in Template

Time:10-02

I'm working in an Angular project. I know I can display the version of the app (from package.json), but I'm wanting to display the version of a pkg the app is using (that's under the dependencies.)

Is there a way to show that?
And on top of that, if I have the version set to get the latest minor and/or patch (with ~ or ^), am I able to grab the version of the pkg that was installed?

CodePudding user response:

Try this...

In your enviornment.ts file do the following:

const packageJson = require('../../package.json');

export const environment = {
    ...
    versions: {
        app: packageJson.version,
        angular: packageJson.dependencies['@angular/core'],     
        rxjs: packageJson.dependencies.rxjs,
        ngxtranslate: packageJson.dependencies['@ngx-translate/core'],      
        angularCli: packageJson.devDependencies['@angular/cli'],
        typescript: packageJson.devDependencies['typescript'],
        cypress: packageJson.devDependencies['cypress']
    }
};

Then just import the env wherever you need it and use it like this:

import { environment as env } from '../../../environments/environment';

env.versions.angular
  • Related