AOT build error Cannot read property 'kind' of undefined
Bug Report or Feature Request (mark with an x)
- [x] bug report -> please search issues before submitting
- [ ] feature request
Versions.
Angular CLI: 1.5.0 Node: 8.5.0 OS: darwin x64 Angular: 5.0.1 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router
@angular/cdk: 5.0.0-rc0 @angular/cli: 1.5.0 @angular/flex-layout: 2.0.0-beta.10-4905443 @angular/material: 5.0.0-rc0 @angular-devkit/build-optimizer: 0.0.32 @angular-devkit/core: 0.0.20 @angular-devkit/schematics: 0.0.35 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.8.0 @schematics/angular: 0.1.2 typescript: 2.4.2 webpack-bundle-analyzer: 2.9.1 webpack: 3.8.1
Repro steps.
rm -rf node_modules/ yarn add @angular/cli@latest -D ng build --aot
The log given by the failure.
ERROR in Error: TypeError: Cannot read property 'kind' of undefined
at nodeCanBeDecorated (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:7805:35)
at nodeIsDecorated (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:7825:16)
at Object.nodeOrChildIsDecorated (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:7829:16)
at isDecoratedClassElement (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51668:23)
at isInstanceDecoratedClassElement (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51659:20)
at Object.filter (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:1652:25)
at getDecoratedClassElements (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51641:23)
at generateClassElementDecorationExpressions (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51815:27)
at addClassElementDecorationStatements (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51804:44)
at visitClassDeclaration (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51131:13)
at visitTypeScript (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50972:28)
at visitorWorker (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50785:24)
at sourceElementVisitorWorker (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50817:28)
at saveStateAndInvoke (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50738:27)
at sourceElementVisitor (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50799:20)
at visitNodes (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:49280:48)
Desired functionality.
AOT build should be successful
Mention any other details that might be useful.
The project built fine with AOT enabled with CLI v1.4.9
@MohammedYaseen I'm not sure, but probably using a custom github repo as registry for the dependency typescript in package.json would work. See this for example.
@Axure, Ok, thanks
We are having the same problem and have been stuck on 1.5.0 because of it. In our case it happens even without arrow functions being passed to decorators
The cause in our case is some decorators being applied to properties that also have Angular decorators (usually @Input) applied to them. Everything works in JIT mode but AOT fails. Stepping through in the debugger we can see that the node's parent is gone and there is no null check to prevent it exploding (as mentioned above by another commenter).
We will probably end up removing the custom decorators anyway (can't rely on decorators at all), but until then, we are stuck or will have to hack Typescript.
Any news on whether this is being looked at? Would a PR to Typescript or the CLI be acceptable?
@filipesilva @SanderElias here is a repro for this bug https://github.com/hccampos/angular-cli-repro-8434
In my case it turned out to be the fact that we were using 2 decorators on a function Our code was like this, which should work and is valid
@HostListener('document:keydown.escape', ['$event'])
@dispatch()
onKeydownHandler() {
return this.actions.hideOverlay();
}
Changing to below got rid of the error
@HostListener('document:keydown.escape', ['$event'])
onKeydownHandler() {
this.hideOverlay();
}
@dispatch()
hideOverlay() {
return this.actions.hideOverlay();
}
@el-davo can't if i've class decorator an property decorator as @hccampos said
Error fixed in typescript version 2.7.0-dev.20171201 https://github.com/Microsoft/TypeScript/pull/20314
@filipesilva can you please get a look here? actually blocking build process (aot)
Have a similar issue AOT build wont work, have to use --prod with --aot=fallse ERROR in : TypeError: Cannot read property 'kind' of undefined at nodeCanBeDecorated (node_modules\typescript\lib\typescript.js:7811:36)
Downgrading angular-cli seems to be the only solution at the moment.
I had the same issue with: Angular CLI 1.6.1 Typescript 2.4.2 Angular 5.0.2
Found a solution by changing typescript.js for now.
Replace all the function
function nodeCanBeDecorated(node)
with the following code
function nodeCanBeDecorated(node) {
switch (node.kind) {
case 229 /* ClassDeclaration */:
// classes are valid targets
return true;
case 149 /* PropertyDeclaration */:
// property declarations are valid if their parent is a class declaration.
// return node.parent.kind === 229 /* ClassDeclaration */;
return (node.parent && node.parent.kind === 229) || (node.original && node.original.parent && node.original.parent.kind === 229);
case 153 /* GetAccessor */:
case 154 /* SetAccessor */:
case 151 /* MethodDeclaration */:
// if this method has a body and its parent is a class declaration, this is a valid target.
return node.body !== undefined &&
// && node.parent.kind === 229 /* ClassDeclaration */;
(node.parent && node.parent.kind === 229) || (node.original && node.original.parent && node.original.parent.kind === 229);
case 146 /* Parameter */:
// if the parameter's parent has a body and its grandparent is a class declaration, this is a valid target;
// return node.parent.body !== undefined
// && (node.parent.kind === 152 /* Constructor */
// || node.parent.kind === 151 /* MethodDeclaration */
// || node.parent.kind === 154 /* SetAccessor */)
// && node.parent.parent.kind === 229 /* ClassDeclaration */;
var parent = node.parent || (node.original && node.original.parent);
return parent && parent.body !== undefined &&
(parent.kind === 152
|| parent.kind === 151
|| parent.kind === 154) && parent.parent.kind === 229;
}
return false;
}
Which comes from here, if you compile typescript: https://github.com/shlomiassaf/TypeScript/commit/7017fa2a0a8137279230f04b59a6ec89f6928331
"@angular/cli": "^1.6.1"
Also throw this error
@gmavritsakis Is it angular-cli issue based on your deduction or typescript? I have upgraded all my other libraries including angular and typescript, but issue arise only when I upgrade angular-cli.
If it is angular-cli issue, I strongly recommend you create a pull request with your fix above so that it can be merged into release.
When I try using [email protected] (which contains the fix), I get this error from the Angular CLI:
Could not find local "typescript" package. The "@ngtools/webpack" package requires a local "typescript@^2.0.2" package to be installed.
The dev version string apparently fails this check: https://github.com/angular/angular-cli/blob/master/packages/@ngtools/webpack/src/index.ts#L9
Can verify that it's angular-cli issue. Rollback to v1.6.0-beta.0 proved that.
I downgraded to angular-cli 1.5.0 and error is gone.
Is there any version of ngtools/webpack without this error?
Install the latest CLI release 1.6.3 and install typescript@next.
@danielsogl Your tip solved the issue for me. Thank you!
Is there a fix path where an upgrade to typescript@next isn't required? I would think this is a angular-cli regression issue since downgrading avoids the error