【问题标题】:Grunt Typescript can't find angular coreGrunt Typescript 找不到角核心
【发布时间】:2016-05-08 21:44:30
【问题描述】:

问题

为什么我的 Grunt Typescript 编译器找不到角核心?

我猜它与路径有关,因此编译器无法在 node_modules 目录中找到库。

错误

typescript/add.component.ts(1,25):错误 TS2307:找不到模块 'angular2/core'。

设置

Gruntfile.js 任务

typescript: {
    all: {
        src: ['typescript/**/*.ts'],
        dest: 'javascript/frontend',
        options: {
            target: "es5",
            module: "system",
            moduleResolution: "node",
            emitDecoratorMetadata: true,
            experimentalDecorators: true,
            removeComments: false,
            noImplicitAny: false
        }
}

typescript/add.component.ts

import {Component} from 'angular2/core';

@Component({
    selector: 'mytest',
    template: '<h1>test</h1>'
})
export class AppComponent { }

node_modules

  • 包括 angular2
  • 包括打字稿

文件路径

app -- node_modules
    -- typescript
         -- app.component.ts
    -- Gruntfile.js
    -- package.json

使用的库/框架/教程

【问题讨论】:

  • 我是 Angular2 n00b,但我看到的大多数示例都使用 Gulp。我们目前正在为我们的构建寻找 Webpack。

标签: angularjs gruntjs typescript npm angular


【解决方案1】:

刚才我遇到了同样的问题。在详细模式下运行 grunt 会显示它从 grunt 配置生成的 ts 配置文件的内容。仔细观察,这表明根本没有使用 moduleResolution 选项。但是,另一方面,官方 grunt-typescript 页面上也没有描述它。

无论如何,长话短说:我改用了grunt-ts 包,一切正常!为了您的方便,我在下面发布了我的配置:-)

module.exports = function(grunt) {

  grunt.initConfig({
    ts: {
      base: {
        src: ['src/**/*.ts'],
        dest: 'dist',
        options: {
          module: 'system', 
          moduleResolution: 'node',
          target: 'es5',
          experimentalDecorators: true,
          emitDecoratorMetadata: true,
          noImplicitAny: false
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-ts');


  grunt.registerTask('default', ['ts']);
};

【讨论】:

  • 泰。今天试一试。
  • 当我运行 'tsc' 命令时,它工作正常,但是当我运行 grunt 时使用与你相同的配置,我得到如下内容:app/main.ts(1,1): error TS6053: File '/Users/jbleach/workspaces/nox/noxApp/WebContent/app/node_modules/angular2/typings/browser.d.ts' not found. node_modules/angular2/platform/browser.d.ts(77,90): error TS2304: Cannot find name 'Promise'. node_modules/angular2/src/common/pipes/async_pipe.d.ts(25,38): error TS2304: Cannot find name 'Promise'. node_modules/angular2/src/core/application_ref.d.ts(83,60): error TS2304: Cannot find name 'Promise'.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 2019-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多