首页 > 解决方案 > 通过 gulp 运行时,Typescript 抛出“无法读取未定义的属性‘长度’”

问题描述

我正在尝试在 Gulp 中构建一个使用 Typescript 的项目。但是,当我尝试这样做时npm run gulp,会引发以下错误:

[09:03:17] Using gulpfile ~/Programming/Projects/ghars/gulpfile.js
[09:03:17] Starting 'default'...
[09:03:19] 'default' errored after 2.25 s
[09:03:19] TypeError: Cannot read property 'length' of undefined
    at Object.emit [as writeFile] (/home/bruhmoment/Programming/Projects/ghars/node_modules/gulp-typescript/release/compiler.js:81:33)
    at Object.writeFile (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:12099:14)
    at emitBuildInfo (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:86317:16)
    at emitSourceFileOrBundle (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:86284:13)
    at forEachEmittedFile (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:86071:28)
    at Object.emitFiles (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:86263:9)
    at emitWorker (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:92213:33)
    at /home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:92174:66
    at runWithCancellationToken (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:92264:24)
    at Object.emit (/home/bruhmoment/Programming/Projects/ghars/node_modules/typescript/lib/typescript.js:92174:20)

npm ERR! Linux 4.15.0-62-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "run" "gulp"
npm ERR! node v8.10.0
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! ghars@1.0.0 gulp: `node node_modules/gulp/bin/gulp.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ghars@1.0.0 gulp script 'node node_modules/gulp/bin/gulp.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ghars package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node node_modules/gulp/bin/gulp.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs ghars
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls ghars
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/bruhmoment/Programming/Projects/ghars/npm-debug.log

我尝试输入一条console.log语句typescript.js来尝试查看正在处理哪些文件,但似乎所有明显的文件都已编译。我不确定我必须做什么来解决这个错误,因为我不熟悉 Typescript。

这是我的 gulpfile.js:

var gulp = require('gulp');
var ts = require('gulp-typescript');
var tsProject = ts.createProject('tsconfig.json');

gulp.task('default', function () {
    return tsProject.src()
        .pipe(tsProject())
        .js.pipe(gulp.dest('dist'));
});

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./",
    "allowJs": true,
    "incremental": true,
    "target": "es5",
    "removeComments": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "strict": true,
    "strictNullChecks": false
  },
  "include": [
    "server"
  ]
}

标签: typescriptnpmgulp

解决方案


关闭“增量”:true,


推荐阅读