I am totally new in Angular 4 and trying to make clear basic structure of a simple project. I already test my project and it works good yesterday. But today when I open my project and want to do "ng serve" it shows below error. Don't know what is the problem.
TypeError: Cannot read property 'length' of undefined
at createSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:15457:109)
at parseSourceFileWorker (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:15389:26)
at Object.parseSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:15338:26)
at Object.createSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:15192:29)
at VirtualFileStats.getSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\@ngtools\webpack\src\compiler_host.js:66:35)
at WebpackCompilerHost.getSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\@ngtools\webpack\src\compiler_host.js:213:38)
at findSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:67909:29)
at processImportedModules (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:68056:25)
at findSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:67937:17)
at processSourceFile (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:67840:27)
at processRootFile (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:67728:13)
at C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:67018:60
at Object.forEach (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:1449:30)
at Object.createProgram (C:\Users\IR\Desktop\angular-first-project\node_modules\typescript\lib\typescript.js:67018:16)
at AotPlugin._setupOptions (C:\Users\IR\Desktop\angular-first-project\node_modules\@ngtools\webpack\src\plugin.js:129:28)
at new AotPlugin (C:\Users\IR\Desktop\angular-first-project\node_modules\@ngtools\webpack\src\plugin.js:26:14)
Thanks in advance.
To fix the “cannot read property of undefined” error, use the optional chaining operator on the variable before accessing a property. If the variable is undefined or null , the operator will return undefined immediately and prevent the property access.
This message indicates that our code expects to have an object with a length property, but that object was not present. length is commonly used on string and array , but a custom object could also have this property. This is a blocking error, and script execution will stop when this error occurs.
The "Cannot read properties of undefined" error occurs when trying to access a property on an undefined value. You often get undefined values when: accessing a property that does not exist on an object. accessing an index that is not present in an array.
The "Cannot read property '0' of undefined" error occurs when trying to access the 0th index in a variable that stores an undefined value. Make sure to initialize the variable to the correct type, e.g. array or string, before accessing the index.
I have seen this types of errors at random occasions, e.g. after moving components to different folders, even after all references was chagned to match a new path.
Rebuilding project with ng-build should help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With