DEV Community

Jonathan Flower
Jonathan Flower

Posted on

Display Angular 12 App Version

Displaying the app version automatically pulled from package.json is a great way to ask clients and users to verify what version they are using. It is also helpful to include it in error logging. In previous versions of Angular it was as easy as doing this inside your environment.ts files:

import { version } from '../../package.json';
Enter fullscreen mode Exit fullscreen mode

However in Angular 12 you get an error:

Error: Should not import the named export 'version' (imported as 'version') from default-exporting module (only default export is available soon)

Solution:

export const environment = {
  ...
  VERSION: require('../../package.json').version,
};
Enter fullscreen mode Exit fullscreen mode

And there is a good chance you will need to add this to your tsconfig.app.json:

"types": [ "node" ]
Enter fullscreen mode Exit fullscreen mode

ref: https://stackoverflow.com/a/68653103/2686911

Discussion (0)