我正在尝试将Angular JavaScript(ES5)项目逐步迁移到TypeScript 2.0,并且必须承认我非常挣扎。
所以首先我将index.js更改为index.ts并使用recommended method of installing typings(npm install --save @types/node
)而不是旧的typings.json方式。
构建设置使用gulp
和browserify
,现在按照建议here引入tsify
和babelify
。
browserify
//...
.plugin(tsify)
.transform(babelify, {
presets: ['es2015'],
extensions: ['.ts', '.js']
})
//...
tsconfig.json
{
"files": [
"assets/app/index.ts"
],
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
然而,构建失败了:
TypeScript错误:.tmp / public / app / index.ts(4,15):错误TS2304:找不到名称'require'。
tsify
似乎没有将已安装的打字复制到输出目录,这会导致上述错误
有关如何解决问题的任何建议?
修改
将 node_modules / @ types 文件夹复制到我的C:驱动器的根目录可以消除错误,但我不知道为什么......
答案 0 :(得分:1)
In case someone runs into the same problem:
Explictly adding the typesRoot
option in tsconfig.json fixed it.
"typeRoots" : ["./node_modules/@types"]
This "should" be the default setting but for some reason it searches for the @types folder under C: drive root.
From TypeScript docs:
If typesRoots is specified, only packages under typeRoots will be included.