我刚刚使用 typescript 启动了一个新的 Nodejs 项目。我安装了打字(https://github.com/typings/typings https://github.com/typings/typings)并使用它来安装 Node v4.x 和 Express v4.x 的参考文件。
我的节点版本是v4.2.6
我的打字稿版本是v1.7.5
我的项目目录是这样布局的:
package.json
tsconfig.json
typings.json
src/
app.ts
typings/
main.d.ts
main/ambient/node/node.d.ts
main/ambient/express/express.d.ts
typings/main.d.ts的内容如下:
/// <reference path="main/ambient/express/express.d.ts" />
/// <reference path="main/ambient/node/node.d.ts" />
tsconfig.json内容如下:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs"
}
}
Typings.json 的内容如下:
{
"dependencies": {},
"devDependencies": {},
"ambientDependencies": {
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#dd4626a4e23ce8d6d175e0fe8244a99771c8c3f2",
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#1c56e368e17bb28ca57577250624ca5bd561aa81"
}
}
src/app.ts内容如下:
'use strict';
///<reference path="../typings/main.d.ts"/>
import * as express from "express";
这非常简单,应该会生成一个基本的应用程序。但是,当我尝试编译它时,我收到错误error TS2307: Cannot find module 'express'.
我尝试重新排列类型文件,更改参考路径标记中的相对路径,使用files
tsconfig.json 中的字段来指示引用路径,而不是在文件中使用内联标记,但一切都无济于事。我也尝试过使用编译gulp-typescript
, gulp-tsc
, and tsc
直接在命令行上。
当我尝试使用 Nodejs 内置模块时,我遇到类似的错误,例如crypto
, http
, fs
etc.
这些参考资料似乎有效——我错过了什么?