示例 webpack angular4 应用程序抛出错误

sample webpack angular4 application throwing errors

我正在尝试创建一个示例 angular-webpack 应用程序,在 https://angular.io/docs/ts/latest/guide/webpack.html 中进行了说明,但它向我抛出了大约 26 个错误。即使将打字稿版本更改为“~2.2.1”,这些错误也会出现。

谁能帮我解决这些错误?

[at-loader] Checking finished with 26 errors                                                                        chunk    {0} vendor.js
(vendor) 2.64 MB {2} [initial] [rendered]
chunk    {1} app.js, app.css (app) 3.57 kB {0} [initial] [rendered]
chunk    {2} polyfills.js (polyfills) 577 kB [entry] [rendered]

WARNING in ./~/@angular/core/@angular/core.es5.js
5870:15-36 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@angular/core/@angular/core.es5.js
5886:15-102 Critical dependency: the request of a dependency is an expression

ERROR in [at-loader] ./node_modules/@angular/common/src/platform_id.d.ts:8:42
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/common/src/platform_id.d.ts:9:41
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/common/src/platform_id.d.ts:10:45
    TS1039: Initializers are not allowed in ambient contexts.
ERROR in [at-loader] ./node_modules/@angular/common/src/platform_id.d.ts:11:44
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/compiler/src/util.d.ts:8:36
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/core/src/animation/animation_metadata_wrapped.d.ts:12:33
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/core/src/animation/dsl.d.ts:34:33
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/core/src/errors.d.ts:9:33
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/core/src/errors.d.ts:10:43
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/core/src/errors.d.ts:11:42
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/core/src/errors.d.ts:12:43
    TS1039: Initializers are not allowed in ambient contexts.
ERROR in [at-loader] ./node_modules/@angular/core/src/errors.d.ts:13:35
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/core/testing/src/before_each.d.ts:1:59
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/http/src/backends/browser_jsonp.d.ts:1:33
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/platform-browser/src/dom/dom_renderer.d.ts:14:41
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./node_modules/@angular/router/src/shared.d.ts:15:37
    TS1039: Initializers are not allowed in ambient contexts.

ERROR in [at-loader] ./src/app/app.component.spec.ts:3:1
    TS2304: Cannot find name 'describe'.

ERROR in [at-loader] ./src/app/app.component.spec.ts:4:3
    TS2304: Cannot find name 'beforeEach'.

ERROR in [at-loader] ./src/app/app.component.spec.ts:7:3
    TS2304: Cannot find name 'it'.

ERROR in [at-loader] ./src/app/app.component.spec.ts:9:5
    TS2304: Cannot find name 'expect'.

ERROR in [at-loader] ./src/app/app.component.ts:5:13
    TS2304: Cannot find name 'require'.

ERROR in [at-loader] ./src/app/app.component.ts:6:12
    TS2304: Cannot find name 'require'.

ERROR in [at-loader] ./src/main.ts:4:5
    TS2304: Cannot find name 'process'.

ERROR in [at-loader] ./src/polyfills.ts:3:1
    TS2304: Cannot find name 'require'.

ERROR in [at-loader] ./src/polyfills.ts:5:5
    TS2304: Cannot find name 'process'.

ERROR in [at-loader] ./src/polyfills.ts:10:3
    TS2304: Cannot find name 'require'.
Child html-webpack-plugin for "index.html":
    chunk    {0} index.html 325 bytes [entry] [rendered]
Child extract-text-webpack-plugin:
    chunk    {0} extract-text-webpack-plugin-output-filename 1.92 kB [entry] [rendered]
webpack: Failed to compile.

TSXXX 错误似乎与使用 2.1 版之前的 TypeScript 相关,如前所述 例如 here and .

请验证:

  • 您在 package.json 文件中声明的 TypeScript 版本("typescript":“~2.2.1”即​​可)
  • 您有效用于编译项目的 TypeScript 版本。您可以从 npm start 命令的输出中验证这一点。你应该看到这样的东西 [at-loader] Using typescript@2.0.10 from typescript and "tsconfig.json" from /usr/src/ng2-projects/angular-webpack/src/tsconfig.json; typescript@ 部分将显示用于编译的版本。

TS2304 错误似乎与所需类型声明的错误配置有关。有关如何配置它们的清晰说明,请参阅

我遇到了同样的问题。我更改为打字稿 2.3("typescript":package.json 中的“~2.3”),现在我不再收到错误了。

我 运行 也遇到了这个问题.. 花了 2 天时间并在一些帮助下我能够让它工作。实际上,我不能把修复的全部功劳都归功于别人,确实帮了很多忙。

  1. 检查您系统上安装的打字稿版本。
  2. 比较您在 package.json 中的版本。

我不记得我是否更新了打字稿,但当时它是 2.2.2,所以我更新了 package.json 以反映我系统上安装的版本。

对我有用。

也许它会对某人有所帮助: 关于打字稿,我有很多不同的错误。我将 tsconfig.json 移动到根文件夹,错误消失了。