2016-12-01 58 views
1

我試圖去抓住用打字稿2的WebPack和角1.5,但建設時的WebPack不斷抱怨它:說服的WebPack使用node_modules/@類型

無法解析模塊「@類型/角'in C:\ play \ gt \ src

我很難過。一直在谷歌搜索和搞亂,但我無處可去。

我app.ts樣子:

import * as angular from "@types/angular"; 
let app = angular.module ("GT.module", []); 

我tsconfig樣子:

{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "target": "es5", 
    "sourceMap": true, 
    "typeRoots" : [ 
     "node_modules/@types" 
     ] 
    }, 
    "exclude": [ 
    "node_modules", 
    "**/*.spec.ts" 
    ], 
    "include" : [ 
    "src/**/*" 
    ] 
} 

...,最後的WebPack配置:

module.exports = { 
     entry: './src/app.ts', 
     output: { 
      filename: './dist/app.js' 
     }, 
     devtool: "source-map", 
     resolve: { 
      extensions: ['', '.webpack.js', '.web.js', '.ts', '.js'], 
      modulesDirectories: ["node_modules"] 
     }, 
     module: { 
      loaders: [ 
       { test: /\.ts$/, loader: 'ts-loader' } 
      ] 
     } 
    } 

回答

1

@types模塊是不是你可以導入,它們是供編譯器參考的typedef。

@types/angular包實際上並沒有實現.module並且您的代碼將會中斷(如果它曾經編譯過)。

你可能想要做更多的東西一樣:

/// <reference path="../node_modules/@types/angular/index.d.ts" /> 
import * as angular from "angular"; 
let app = angular.module ("GT.module", []); 

,你通常會甚至不需要reference,因爲編譯器會自動包括@types/*包的typedef,除非你告訴它不要(通過在您的tsconfig.json中設置typestypeRoot)。此行爲已添加到v2中,並且相當新近。

2

你的目錄樹是什麼樣的? 它應該是這樣的:

node_modules/ 
    @types/ 
     angular/ 
src/ 
tsconfig.json 
webpack.config.js 

那麼你不需要"typeRoots" : ["node_modules/@types"]

import * as angular from "@types/angular";應該成爲import * as angular from "angular";

打字稿編譯器2.0將知道去哪裏找找到在正確的分型地方(即:在node_modules/@types

+0

謝謝!這已經解決了它。 Typescript目標帖子似乎在不停地移動,很難找到最新的文章。 – jeffeld

+0

是啊,我知道,JavaScript生態系統發展得非常快,嘗試官方文檔,它保持相當不錯。例如,這個鏈接將使用typescript包裝webpack:https://www.typescriptlang.org/docs/handbook/react-&-webpack.html –

相關問題