在angular2应用程序中运行ng服务时收到错误'无法读取未定义的属性'长度'

时间:2016-12-14 07:17:07

标签: node.js angular typescript angular-cli

在使用angular-cli命令ng-serve运行angular2应用程序时,我遇到了以下问题

  

dashboard@0.0.0启动C:\ Users \ padmavathi \ Downloads \ phototype-development \ web \ dashboard   服务

Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at createSourceFile (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\typescript\lib\typescript.js:8980:109)
    at parseSourceFileWorker (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\typescript\lib\typescript.js:8940:26)
    at Object.parseSourceFile (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\typescript\lib\typescript.js:8899:26)
    at Object.createSourceFile (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\typescript\lib\typescript.js:8727:29)
    at new TypeScriptFileRefactor (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\@ngtools\webpack\src\refactor.js:25:35)
    at Object.resolveEntryModuleFromMain (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\@ngtools\webpack\src\entry_resolver.js:108:18)
    at AotPlugin._setupOptions (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\@ngtools\webpack\src\plugin.js:129:58)
    at new AotPlugin (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\@ngtools\webpack\src\plugin.js:37:14)
    at Object.exports.getWebpackNonAotConfigPartial (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\angular-cli\models\webpack-build-typescript.js:20:13)
    at new NgCliWebpackConfig (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\angular-cli\models\webpack-config.js:23:42)
    at Class.run (C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\angular-cli\tasks\serve-webpack.js:20:22)
    at C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\node_modules\angular-cli\commands\serve.js:102:26
    at process._tickCallback (internal/process/next_tick.js:103:7)

npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.9.1
npm ERR! npm  v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! dashboard@0.0.0 start: `ng serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the dashboard@0.0.0 start script 'ng serve'.
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 dashboard package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     ng serve
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs dashboard
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls dashboard
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\padmavathi\Downloads\phototype-development\web\dashboard\npm-debug.log.

请帮我解决这个问题。 提前致谢。

5 个答案:

答案 0 :(得分:2)

我们在重构过程中意外删除了environment.prod.ts时遇到了同样的错误(while($row =mysqli_fetch_assoc($result)) { $emparray[] = array_map('utf8_encode', $row); } )。恢复此文件解决了我们的问题。

PS。 Github还报道了一个问题:

答案 1 :(得分:2)

有同样的问题。

我添加了

console.log("createSourceFile " + fileName);
位于createSourceFile

下的\node_modules\typescript\lib\typescript.js函数中的

再次运行ng build,记录所有文件。 我设法看到缺少哪个文件,并处理它。

答案 2 :(得分:1)

引用github(因为我不知道如何直接链接回复:“>)

  

我认为在很多情况下,当typescript尝试处理不存在的文件时会导致此问题。在我的一个项目中,它是一个缺少的.d.ts文件。在另一个它是一个缺少环境ts文件。在node_modules / typescript / lib / typescript.js的createSourceFile函数的开头添加console.log(fileName)帮助我查明特定文件。请记住删除更改或只是重新安装node_modules。

来源:https://github.com/angular/angular-cli/issues/5053

这对我有用。最后一个console.log显示了麻烦的文件

答案 3 :(得分:1)

检测错误的简便方法正在运行 tsc -p< file_tsconfig.json_path> 您将在编译源代码期间看到完整错误。

答案 4 :(得分:0)

我也遇到了这个问题。我检查了environment.ts

中的所有angular-cli.json个文件和其他文件路径

在我做出任何改变后,Angular拒绝重建。在尝试下面的修复之前,我尝试了以下操作:

  • 使用rm -rf node_modules/*清除node_modules,然后执行全新的npm安装

  • 重新克隆整个仓库

最后解决我的问题的唯一方法是将@ngtools/webpack插件从版本1.9.0降级为1.8.2

我不确定这是否是正确的解决方案,但它对我有用。我在这里添加这个评论,因为它可能会节省一些时间。

我的配置:

Mac OSX 10.11.6
"@angular/cli": "1.4.2"
"typescript": "~2.3.3"
"webpack": "~3.5.5"