2016-03-03 74 views
1

我有一個項目,我基於角2種子存儲庫(https://github.com/mgechev/angular2-seed),我試圖添加一個ExpressScrip服務器後端編寫在TypeScript中。我的目錄結構是相同的,除了我添加了一個名爲server/src/的文件夾。編譯時TypeScript找不到外部模塊

我已經運行typings install,我可以看到,express.d.ts是在typings/目錄,但由於某種原因,我的編譯代碼時,我(使用[email protected])總是出現以下錯誤:

> npm start 

> [email protected] start C:\Users\Cody\projects\angular2-seed 
> tsc --outDir build/ --module commonjs ./src/server/server.ts && node ./build/server.js 

src/server/server.ts(1,26): error TS2307: Cannot find module 'express'. 

./src /server/server.ts:

import * as express from 'express'; 

let app = express(); 

app.get('/', function(req, res) { 
    res.send('Hello World'); 
}); 

app.listen(3000, 'localhost'); 
console.log('Listening on port 3000'); 

奇怪的是在服務器運行時不抱怨,如果我使用TS-節點

> ts-node ./src/server/server.ts 
Listening on port 3000 

但我不打算在生產中使用ts-node,因爲擔心性能問題(不確定這是否合理)。

爲什麼編譯器找不到快速外部模塊?我對使用TypeScript非常新,所以我們不勝感激。

**編輯**

tsconfig.json:

{ 
    "compilerOptions": { 
     "target": "es5", 
     "module": "commonjs", 
     "declaration": false, 
     "noImplicitAny": false, 
     "removeComments": true, 
     "noLib": false, 
     "emitDecoratorMetadata": true, 
     "experimentalDecorators": true, 
     "sourceMap": true 
    }, 
    "files": [ 
     "typings/main.d.ts" 
    ], 
    "exclude": [ 
     "node_modules", 
     "typings/browser.d.ts", 
     "typings/browser/**" 
    ], 
    "compileOnSave": false 
} 

typings docs說,你只需要或者排除一個或包括其他的,但我都嘗試,它仍然沒有工作。

回答

0

原來我用不是我已通過NPM安裝了一個打字稿編譯器的版本,但通過Visual Studio安裝,是舊版(1.0版)。您可以通過運行where tsc進行檢查。

解決方案是從我的Path env變量中刪除C:\ Program Files(x86)\ Microsoft SDKs \ TypeScript 1.0條目。

根據this GItHub issue,它看起來像這是官方接受的解決方案。

1

但我不打算在生產中使用ts-node,以免擔心性能問題(不確定這是否合理)。

合理。雖然只是輕微。它只是您保存的初始編譯成本。

爲什麼編譯器找不到快速外部模塊?

確保您tsconfig.json是否設置正確包括typings/main.d.ts

+0

感謝您的回答,但請參閱我的編輯。也感謝關於ts-node的見解。如果只是在一些編譯時間上添加,那麼它畢竟可能值得使用。 –

+0

你能分享你的項目嗎? ng2-seed感覺過於複雜:) – basarat