2016-03-16 27 views
16

我想在我的應用程序中使用expressjs。如何安裝快遞類型?

使用typings install express --ambient --save安裝它後,我跑tsc,但我得到兩個錯誤:

typings/main/ambient/express/index.d.ts(17,34): error TS2307: Cannot find module 'serve-static'. typings/main/ambient/express/index.d.ts(18,27): error TS2307: Cannot find module 'express-serve-static-core'.

所以,我試圖同時安裝:

typings install serve-static --ambient --save 
typings install express-serve-static --ambient --save 

,然後我再次運行TSC,但得到一個更多的錯誤:

typings/main/ambient/serve-static/index.d.ts(79,24): error TS2307: Cannot find module 'mime'.

我該如何解決這些問題?我如何自動安裝Express的所有依賴關係?

回答

8

我只是碰到了這個自己,我相信是一個重複:

Importing node and express with typings in TypeScript

我安裝了服務於靜態和表達發球靜態然後得到錯誤,說明我缺少「啞劇」和'http'。

我不得不安裝節點類型來解決缺少的http引用和MIME類型來解決MIME缺少的引用。

typings install mime --ambient --save 
typings install node --ambient --save 
+8

隨着分型1.0,我相信這是現在:'分型安裝DT〜啞劇--global --save' –

+0

現在(OCT 2016)正確的命令是'分型安裝mime --global --save --source dt' –

10
{ 
    "globalDependencies": { 
    "express": "registry:dt/express#4.0.0+20160708185218", 
    "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160715232503", 
    "mime": "registry:dt/mime#0.0.0+20160316155526", 
    "node": "registry:dt/node#6.0.0+20160621231320", 
    "serve-static": "registry:dt/serve-static#0.0.0+20160606155157" 
    } 
} 

這是我的工作Typings.json

+1

這整個時間....在我typings.json爲我的錯誤: - >「註冊表:dt/express-serve-static-core#0.0.0 + 20160715232503」不知怎的,我得到了0.0.0版本,並且必須手動將其更改爲4.0.0,然後運行 typings install dt〜express-serve-static-core --global --save –

+0

爲什麼不將它們與express一起安裝?這些同伴依賴關係嗎? – d512

5

爲我工作(那天我貼)是命令: typings install dt~express --global --save (環境被全球所取代)

要找到其他相關模塊你可以使用命令typings search express(它也給你來源信息)

17

機智^ h打字稿2.0(https://blogs.msdn.microsoft.com/typescript/2016/09/22/announcing-typescript-2-0/),現在不同了:

如果安裝打字稿用下面的命令:

npm install -g [email protected]

你將不得不使用命令安裝快遞分型

npm install --save @types/express

代替類型在早期版本中使用環境/全局進行安裝。該分型得到安裝在node_modules/@types/express目錄

你的package.json做npm installtypes後,將有以下片斷:

"dependencies": { 
    "@types/express": "^4.0.33" 
    } 
+0

「@ types/express」的版本必須與「express」的版本相同? – Jas

0

心中已經遇到這個問題我自己,發現你還必須有安裝實際的nodeJS模塊以及鍵入

因此,當你有correclty配置typescript和你的項目,你需要安裝nodeJS依賴以及@types依賴。

npm install express --save

npm install --save @types/express