2016-11-04 136 views
4

我正在嘗試結合Angular1.5 + Typescript。 安裝需要類型:AngularJs + Typescript:錯誤TS2307:找不到'angular'模塊

npm install @types/angularjs

,但仍然看到錯誤:

error TS2307: Cannot find module 'angular'

已經嘗試過不同的選擇:

import 'angular'; 
import 'angularjs'; //that is name inside @types 
import angular from 'angularjs'; 
import * as angular from 'angular'; 

我使用:

"ts-loader": "1.0.0", 
"typescript": "2.0.7", 
"webpack": "1.13.3", 
"webpack-dev-server": "1.16.2" 

tsconfig是非常標準:

{ 
    "compilerOptions": { 
    "target": "es5", 
    "noImplicitAny": false, 
    "sourceMap": false, 
    "lib": ["es6", "dom"], 
    "typeRoots": ["./node_modules/@types"] 
    }, 
    "exclude": ["node_modules"] 
} 

已經簡化的WebPack配置儘可能: VAR的WebPack =新要求( '的WebPack');

module.exports = { 
    context: __dirname + '/src', 
    entry: './index.ts', 
    output: { 
    path: './dist', 
    filename: 'app.bundle.js' 
    }, 
    module: {loaders: [{test: /\.tsx?$/, loader: 'ts-loader'},]}, 
    resolve: { 
    extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js'], 
    }, 

    plugins: [new webpack.NoErrorsPlugin()], 

    devtool: 'source-map', 

    devServer: { 
    contentBase: './dist', 
    historyApiFallback: true, 
    inline: true 
    } 
}; 

而且我也得到了簡化index.ts本身,只注重進口類型:

import * as angular from 'angular'; 
angular.module('testmodule', []); 

UPD:tsconfig.json。增加了typeRoots選項。

現在獲得新的錯誤:

ERROR in .../node_modules/@types/angularjs/angular-mocks.d.ts (6,26): error TS2307: Cannot find module 'angular'.

ERROR in .../node_modules/@types/angularjs/angular-mocks.d.ts (65,49): error TS2304: Cannot find name 'IServiceProvider'.

ERROR in .../node_modules/@types/angularjs/angular-mocks.d.ts (122,62): error TS2304: Cannot find name 'IScope'.

ERROR in .../node_modules/@types/angularjs/index.d.ts (66,43): error TS2304: Cannot find name 'JQuery'.

+0

你可以顯示你的目錄結構和tsconfig文件嗎?另外,如果您使用的是像Webpack或SystemJS這樣的工具,那麼也可以配置它。 –

+0

感謝您的反饋,更新了我的問題 –

+0

嘗試在您的tsconfig中將''typeRoots「:[[./node_modules/@types]]'添加到''compilerOptions」'' –

回答

3

,如果你在命令行中運行這個應該解決這個問題。

npm install @types/angular 

什麼惱人的有關此修復程序是出於某種原因,如果你添加以下到你的依賴的package.json 內的上面,而不是應該修復它不會更新您的package.json。但是問題並使package.json鏡像您的包。

"@types/angular": "1.6.5", 
"@types/jquery": "2.0.40", 
+4

'由於某些原因,它不會更新你的package.json' - 添加'--save'標誌爲了將它保存在npm5的package.json –

+0

中,npm將默認保存--save。 – Prashant

相關問題