我有一個項目,我基於角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說,你只需要或者排除一個或包括其他的,但我都嘗試,它仍然沒有工作。
感謝您的回答,但請參閱我的編輯。也感謝關於ts-node的見解。如果只是在一些編譯時間上添加,那麼它畢竟可能值得使用。 –
你能分享你的項目嗎? ng2-seed感覺過於複雜:) – basarat