使用browserify + babelify + tsify缺少TypeScript类型

时间:2016-10-20 11:10:22

标签: typescript gulp browserify tsify

我正在尝试将Angular JavaScript(ES5)项目逐步迁移到TypeScript 2.0,并且必须承认我非常挣扎。
所以首先我将index.js更改为index.ts并使用recommended method of installing typingsnpm install --save @types/node)而不是旧的typings.json方式。

构建设置使用gulpbrowserify,现在按照建议here引入tsifybabelify

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:驱动器的根目录可以消除错误,但我不知道为什么......

1 个答案:

答案 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.