[Solved] Angular12 ng build Error: Index html generation failed.

When using angular12 (version 12.0.3) to develop a project, the debugging will run normally, but an error message will appear when calling ng build to publish.

Tip: index HTML generation failed undefined:6:720366 missing ‘}’

Or: index HTML generation failed Undefined: 6:720366 missing ‘:’ etc.

The solution is to modify the angular.json configuration file, Add an optimization node under the build -> configurations ->production node:

            "optimization": {
                "scripts": true,
                "fonts": {
                  "inline": true
                },
                "styles": {
                  "minify": true,
                  "inlineCritical": false
                }
              }

The complete node path is:

{
    ...,
    "projects":{
        "myapp":{
           ...,
           "architect":{
               ...,
                "build":{
                    ...,
                    "configurations":{
                        ...,
                        "production": {
                            ..., 
                            "optimization": {
                                "scripts": true,
                                "fonts": {
                                  "inline": true
                                },
                                "styles": {
                                  "minify": true,
                                  "inlineCritical": false
                                }
                              }                       
                        }                                           
                    },
                    ...                                    
                },
                ...                          
           },
           ...         
        }            
    },
    ...
}

Read More: