2016-03-01 68 views
1

我正在嘗試使用新的角度2.0測試版自動化打字稿編譯。Angular2 beta和gulp-tsc - 找不到模塊'angular2/core'

項目結構是:

myapp 
|--node_modules 
    |---angular2 
      |--core.d.ts 
      |--... 
|--lib 
    |--resources 
     |--app 
      |--app.component.ts 
|--typings 
    |--.. 
. 
|--package.json 
|--gulpfile.js 

app.component.ts摘錄:

import {Component, View} from 'angular2/core'; 
import {TechnologiesService} from './services'; 

當我運行打字稿命令(TSC)直接從殼一切順利並生成JavaScript文件。但是,當我運行我的gulp編譯任務時,有一些錯誤,因爲它沒有找到angular2/core和angular2/platform/browser模塊。爲什麼?

[16:35:55] Using gulpfile C:\dev\myapp\gulpfile.js 
[16:35:55] Starting 'compile-ts'... 
[16:35:55] Compiling TypeScript files using tsc version 1.8.2 
[16:35:56] [tsc] > lib/resources/app/app.component.ts(1,46): error TS2307: Cannot find module 'angular2/core'. 
[16:35:56] [tsc] > lib/resources/app/app.component.ts(44,14): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. 
[16:35:56] [tsc] > lib/resources/app/main.ts(1,28): error TS2307: Cannot find module 'angular2/platform/browser'. 
[16:35:56] Failed to compile TypeScript: Error: tsc command has exited with code:2 

events.js:154 
     throw er; // Unhandled 'error' event 
    ^
Error: Failed to compile: tsc command has exited with code:2 

gulpfile 打字稿編譯任務

var gulp = require('gulp'); 
var plugins = require('gulp-load-plugins')(); 
var typescript = require('gulp-tsc'); 
gulp.task('compile-ts', function(){ 
    return gulp.src(['./lib/resources/app/**/*.ts']) 
    .pipe(typescript({"target": "es5", "module": "system", "moduleResolution": "node", "sourceMap": true, 
         "experimentalDecorators": true, "emitDecoraasdftorMetadata": true, "removeComments": false, 
         "noImplicitAny": false})) 
    .pipe(gulp.dest('./public/app/')); 
}); 
+0

您是否已安裝和包含類型碼?例如。 '/// ' – martin

+0

當我運行npm install時,typings文件夾被創建(myapp/typings)。試着添加'/// '但仍然抱怨angular2/core。還把'/// '並沒有任何變化。 – codependent

+0

我猜這是一個工作目錄問題。你有沒有試過指定rootDir? – Harangue

回答

0

我使用一飲而盡,打字稿,而不是一口-TSC及以下gulpfile.js與指定tsconfig.json文件完美的工作。

var gulp = require('gulp'); 
var ts = require('gulp-typescript'); 
var path = require('path'); 
var sourcemaps = require('gulp-sourcemaps'); 

gulp.task('build:client', function() { 
    var tsProject = ts.createProject('client/tsconfig.json'); 
    var tsResult = gulp.src([ 
     'client/**/*.ts', 
     '!client/typings/main/**/*.ts', 
     '!client/typings/main.d.ts' 
    ])   
     .pipe(sourcemaps.init()) 
     .pipe(ts(tsProject)) 
    return tsResult.js 
     .pipe(sourcemaps.write()) 
     .pipe(gulp.dest('server')) 
}); 

gulp.task('default', ['build:client']); 

我tsconfig.json

{ 
    "compilerOptions": { 
    "target": "es5", 
    "module": "system", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": false, 
    "rootDir":"./"  
    }, 
    "exclude": [  
    "typings/main", 
    "typings/main.d.ts" 
    ] 
} 

要安裝分型

npm install typings --save-dev 

初始化typings.json

typings init 

要爲 「ES6的集合」 安裝分型和「es6-promise」

typings install es6-collections --ambient --save 
typings install es6-promise --ambient --save 
+0

我剛剛測試它,並得到76語義錯誤:'dopangular/node_modules/angular2/platform/browser.d.ts(77,90):錯誤TS2304:找不到名稱'承諾'。 '','/dopangular/node_modules/angular2/src/core/change_detection/differs/default_keyvalue_differ.d.ts(23,15):錯誤TS2304:找不到'Map'等名字。另一方面,雖然sourcemap文件丟失,.js文件正確生成,應用程序運行正常。任何想法爲什麼我得到這些錯誤?爲什麼沒有任何源文件? – codependent

+0

顯示編譯錯誤是因爲它找不到所提到實體的類型。嘗試安裝「es6-collections」和「es6-promise」的環境類型。源代碼被添加到生成的js文件中。 – rmchndrng

+0

我對ts完全陌生......如何安裝環境類型?感謝您幫助我;-) – codependent

相關問題