2016-03-03 72 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