Grunt Typescript 找不到 angular 核心

Grunt Typescript can't find angular core

问题

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

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

错误

typescript/add.component.ts(1,25): error TS2307: Cannot find module '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

文件路径

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

二手libs/frameworks/tutorials

刚才我遇到了同样的问题。 运行 详细模式下的 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']);
};