When debugging an Angular application in the browser, verifying the exact version of the framework running under the hood is often the first step in troubleshooting.
While you could always check your package.json, sometimes you don't have access to the source code, or you need to confirm that the deployed build matches your expectations.
Here are three ways to extract the Angular version directly from the browser, ranging from the absolute fastest methods to digging into optimized Webpack bundles.
1. The Quickest Way: Inspect the DOM
When Angular bootstraps an application, it automatically stamps the root component with the exact framework version. This is the fastest, most reliable method if the application has successfully rendered.
How to do it:
- Open your browser's Developer Tools (
F12or right-click -> Inspect). - Go to the Elements tab.
- Locate the root application tag (usually
<app-root>). - Look for the
ng-versionattribute.
You’ll see something like this: <app-root ng-version="21.2.14"></app-root>. That's it!
2. The Console Approach: Querying the Version
If you prefer keeping your hands on the keyboard and out of the Elements tree, you can use a quick JavaScript snippet in the console to pull the exact same attribute.
How to do it:
- Open the Console tab in your Developer Tools.
- Run the following command:
document.querySelector('[ng-version]').getAttribute('ng-version');
The console will return the version string, such as "21.2.14".
3. The Deep Dive: Searching the Webpack Bundles
Sometimes, you might need to inspect the actual JavaScript bundles—perhaps the app is failing to bootstrap, and the <app-root> tag hasn't been stamped yet.
Older tutorials will tell you to look for a vendor.js file and search for new Version(. However, in modern Angular configurations, this advice is often misleading.
If your angular.json has vendorChunk set to false, or if you are dealing with a build where optimization and output hashing are enabled, Angular doesn't split third-party libraries into a separate vendor file. Instead, everything is bundled into a hashed main file, and the code is minified.
How to find the version in optimized Webpack builds:
- Open the Developer Tools and navigate to the Sources tab (or check the Network tab for loaded JS).
- Look for your main application bundle. It will typically be named something like
main.[hash].js(e.g.,main.3b878c28634a75c9.js). - Open this file and press
Ctrl + F(orCmd + F) to open the in-file search. - Because the code is minified, the
Versionclass has likely been aliased to a single letter (likelore). Instead of searching for the full declaration, search for a partial match: Type
.Version("orVersion("into the search bar.You will find a minified snippet that looks something like this:
new l.Version("21.2.14")
The string inside the quotes is your Angular version.
Summary
Whether you are dealing with local development environments or heavily optimized production builds, knowing how to navigate the DOM and your Webpack bundles is a handy debugging skill. For quick checks, rely on the ng-version attribute. For deeper bundle analysis, just remember to adjust your search terms to account for minification!
Top comments (0)