当我运行“ ng build --prod”时,Angular 6 Build Error

时间:2018-09-28 12:58:36

标签: javascript angular

NG -V

Angular CLI: 6.2.3
Node: 8.11.3
OS: darwin x64
Angular: 6.1.9
... animations, common, compiler, core, forms, http
... platform-browser, platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.8.3
@angular-devkit/build-angular     0.8.3
@angular-devkit/build-optimizer   0.8.3
@angular-devkit/build-webpack     0.8.3
@angular-devkit/core              0.8.3
@angular-devkit/schematics        0.8.3
@angular/cdk                      5.2.5
@angular/cli                      6.2.3
@angular/compiler-cli             6.0.3
@angular/fire                     5.0.2
@angular/language-service         5.2.11
@angular/material                 5.2.5
@angular/service-worker           5.2.11
@ngtools/webpack                  6.2.3
@schematics/angular               0.8.3
@schematics/update                0.8.3
rxjs                              6.3.3
typescript                        2.7.2
webpack                           4.20.2

这是我使用http-server dist /运行dist文件夹时控制台中显示的内容 在本地主机上:8080 现在,由于此错误,整个应用程序无法正常工作。

Uncaught ReferenceError: global is not defined
  at Object.+SKG (main.c956850f3d270f43f2eb.js:1)
  at f (runtime.ec2944dd8b20ec099bf3.js:1)
  at Object.cD5x (main.c956850f3d270f43f2eb.js:1)
  at f (runtime.ec2944dd8b20ec099bf3.js:1)
  at Object.Vo14 (main.c956850f3d270f43f2eb.js:1)
  at f (runtime.ec2944dd8b20ec099bf3.js:1)
  at Object.gFX4 (main.c956850f3d270f43f2eb.js:1)
  at f (runtime.ec2944dd8b20ec099bf3.js:1)
  at Module.zUnb (main.c956850f3d270f43f2eb.js:1)
  at f (runtime.ec2944dd8b20ec099bf3.js:1)

我尝试添加(window as any).global = window;进入polyfills.ts文件,但从未停止。

1 个答案:

答案 0 :(得分:0)

您可以尝试两种可能对您有用的方法。


尝试从polyfills.ts删除这些行

import 'intl';   
import 'intl/locale-data/jsonp/en.js';

您可以在this thread中查看有关此内容的更多信息。


如果由于某种原因该方法不起作用,则可以更新index.html。在结束的</head>标签上方添加以下代码段:

<script>    
  var global = global || window;    
  var Buffer = Buffer || [];    
  var process = process || {    
    env: { DEBUG: undefined },  
    version: [] 
  };    
</script>

第一个解决方案应该对您有用,因为这是在Angular 6中进行的更新。第二个解决方案是我发现的elsewhere on SO并有所帮助,但我认为核心问题在于对intl的更新。

我希望这会有所帮助。