【问题标题】:TypeScript compiler in Gulp says it cannot find @angular modules but compiles fineGulp 中的 TypeScript 编译器说它找不到 @angular 模块但编译得很好
【发布时间】:2016-09-03 08:43:05
【问题描述】:

当我的gulp 任务编译我的打字稿时,我收到了很多“错误”错误。这是相关的gulp task

var tsProject = typescript.createProject('tsconfig.json', {noResolve: true});

gulp.task('build-ts', function() {
    gulp.src(appDev + '**/*.ts')
        .pipe(sourcemaps.init())
        .pipe(typescript(tsProject))
        .pipe(sourcemaps.write())
        //.pipe(jsuglify())
        .pipe(gulp.dest(appProd));
    gulp.src(appDev + '**/*.+(html|css)')
        .pipe(gulp.dest(appProd));
});

这些是我得到的错误:

[09:38:52] Starting 'default'...
[09:38:52] Finished 'default' after 8.88 ms
ng/app.component.ts(1,27): error TS2307: Cannot find module '@angular/core'.
ng/app.component.ts(5,12): error TS2304: Cannot find name 'module'.
ng/app.module.ts(1,26): error TS2307: Cannot find module '@angular/core'.
ng/app.module.ts(2,31): error TS2307: Cannot find module '@angular/platform-browser'.
ng/app.module.ts(3,31): error TS2307: Cannot find module '@angular/common'.
[...]
[09:38:53] TypeScript: 27 semantic errors
[09:38:53] TypeScript: emit succeeded (with errors)
[BS] Proxying: http://0.0.0.0:5000

我的树形结构看起来像这样(简化):

.
├── gulpfile.js
├── ng
├── node_modules
├── package.json
├── tsconfig.json
├── typings.json
└── web

Typescript 编译器找不到的所有模块实际上都在 node_modules 中,尽管有所有错误,我的最终应用程序都可以正常工作。

这是我的tsconfig.js

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "outDir": "./app"
  },
  "filesGlob": [
    "./app/**/*.ts",
    "!./node_modules/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ],
  "atom": {
    "rewriteTsconfig": true
  }
}

我怎样才能摆脱这些“误报”错误。

【问题讨论】:

标签: angular typescript gulp


【解决方案1】:

gulp.js 文件中的行

var tsProject = typescript.createProject('tsconfig.json', {noExternalResolve: true, noResolve: true});

应该是

var tsProject = typescript.createProject('tsconfig.json');

因为我们要加载外部类型。

您还需要在顶部的 ma​​in.ts 文件中加载类型:

/// <reference path="../typings/index.d.ts" />

我确实克隆了您的项目,并且在此之后它编译时没有警告。

【讨论】:

  • 有趣的是,我之前曾独立尝试过这两件事。谢谢!
  • 我之前告诉过你是因为打字,但不知道你的整个配置:)
  • Typings 不应包含在引用中,而应直接包含在 tsconfig 或您的构建链中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多