我有一个项目,我基于角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