RangeError: Maximum call stack size exceeded
It seems incredibly easy to cause an infinite loop in a production Angular 2 app. The following example code is the most simplified reproduction case I could come up with. In dev mode, it displays what you would expect and logs the following text:
Demo.constructor
TopLevelComponent.constructor
SecondLevelComponent.constructor
In production mode the SecondLevelComponent gets recreated until the stack overflows. (The "SecondLevelComponent.constructor" message prints on loop.)
Removing either of the un-used public methods (someMethod) causes the problem to go away.
This is an over-simplification but it's essentially what I'm trying to do for the bvaughn/ng2-virtualized demo app. The app works fine when running locally, in dev-mode. But the static production build (visible here) fails.
Steps to reproduce and a minimal demo of the problem
Here's a Plnkr: https://plnkr.co/edit/5kHjjmkWbYpi7y8nHTqL?p=preview
And here's the source code:
HTML
<html>
<head>
<script src="https://npmcdn.com/[email protected]/bundles/angular2-polyfills.js"></script>
</head>
<body>
<demo>Loading...</demo>
</body>
</html>
JavaScript
import {Component, Input, OnInit} from 'angular2/core'
import {bootstrap} from 'angular2/platform/browser'
@Component({
selector: 'second-level-component',
template: 'SecondLevelComponent'
})
class SecondLevelComponent {
constructor () {
console.log('SecondLevelComponent.constructor')
}
// Note that without both this method and the one below, the error does not happen
// But neither method is actually called
someMethod () {}
}
@Component({
directives: [SecondLevelComponent],
selector: 'top-level-component',
template: `
<h1>TopLevelComponent</h1>
<second-level-component></second-level-component>
`
})
class TopLevelComponent {
constructor () {
console.log('TopLevelComponent.constructor')
}
// Note that without this method (or the one above) the error does not happen
// But neither method is actually called
someMethod () {}
}
@Component({
directives: [TopLevelComponent],
selector: 'demo',
template: '<top-level-component></top-level-component>'
})
class Demo {
constructor () {
console.log('Demo.constructor')
}
}
bootstrap(Demo)
Runtime error
browser_adapter.js:76Error: Uncaught (in promise): RangeError: Maximum call stack size exceeded
at resolvePromise (angular2-polyfills.js:543)
at angular2-polyfills.js:579
at ZoneDelegate.invokeTask (angular2-polyfills.js:365)
at Object.inner.inner.fork.onInvokeTask (ng_zone_impl.js:35)
at ZoneDelegate.invokeTask (angular2-polyfills.js:364)
at Zone.runTask (angular2-polyfills.js:263)
at drainMicroTaskQueue (angular2-polyfills.js:482)
I love the writeup. I wish all errors would be written up so well! Thank you for making our life easy.
This is a known issue and it has to do with minification. In short the minification is broken in Angular as of right now. The fix will come as part of https://github.com/angular/angular/pull/6567 in a week or so. The issue is that when the minfier munges the code the classes get renamed and collide. <second-level-component> then gets added recursively which causes a stack overflow.
here's a current workaround for the bundle build https://github.com/angular/angular/pull/7988 you need to turn off mangle so props could be invoked and allow function names for directives/components/pipes to work in the template
Thanks for the details @mhevery and the workaround @gdi2290 :)
Glad to hear it's a known issue and a fix is on the way. Best outcome I hope for.
FWIW was able to get a working static build made by removing the -p param to Webpack and disabling mangling like so:
new webpack.optimize.UglifyJsPlugin({
mangle: false,
sourceMap: false
})
(Posting in case it helps others, as I don't think Webpack and Angular 2 are a very common pair at the moment.)
the problem has more to do with the JIT template refactor from beta.1 and Uglify than webpack and angular2. You can reproduce the same error with systemjs or anything using Uglify
Yup, that was understood. Sorry. I was just trying to leave steps to fix it behind in case anyone else also using Webpack and Angular 2 encountered this issue. :)
@bvaughn Brian! Thank you! Just wanted to confirm that I am a fellow angular 2/webpack user, and I've been trying to fix this for days, and your solution definitely helped! Thanks!
I'm getting this using Meteor 1.3 and angular 2 rc1. Is the fix in rc1? (i.e. do I have a new issue?)
@kokokenada have you found a solution? I am using angular2 rc.1 with Meteor 1.3 too and I can't make the app work on Safari or any iOS browser because it throws [Error] RangeError: Maximum call stack size exceeded. In other contexts it works fine.
I have found that this happens on any xhr call, after the first one the app simply crashes. It happens when ng2-translate requires the json file from the server and also when angular tries to load any component's HTML template set on templateUrl.
@henrique-rotava - yes I was able to get it going. It's a matter of getting the right magically combination of versions, I think. You should be able to use the package.json in https://github.com/kokokenada/for-real-cards to replicate the setup. It's still at angular rc1 while I wait for the dust to settle from rc2.
@kokokenada, thank you! After I upgraded my meteor packages, removing and adding all of them again, it is finally working.
I am not even minifying any file , still getting this.
Is this bug/issue fixed/resolved? I am getting errors with Third party libraries which are minified.
@ganeshkbhat in my case i was loading some wrong static files. for example :- instead of template html file, i was getting root index.html file and that file upon loading was requesting same resources again and again. so it was going in a loop.
I had this problem. I was using this:
@Component({
templateUrl: './my.html'
})
It resolved when I switched to this:
import template from './my.html';
@Component({
template
})
@dpdonohue and @nnIIInnjA I know my problem is because of usage of minified files. I am using minfied RightJS for tabs to get a small footprint for a dynamic tabbed structure. When I add the files to index.html using script tags it gives this error. But when I put the files to map in systemjs.config the object RightJS is not found 'even' after declaring the object in a .ts file
I had the same problem and SOLVED my problem. I accedentally imported the same module which is TravelsModule in TravelsModule.. :) Recursive import results this probem. I removed that import and my problem is solved. My problem was not related to any minified files.. As an example, I pasted it below and commented wrong line..
@NgModule({
imports: [
CommonModule,
RouterModule,
//TravelsModule, <----- This line causes the problem
],
declarations: [
],
providers: [
],
exports: [
]
})
export class TravelsModule {}
Uncaught RangeError: Maximum call stack size exceeded
at Object.U (http://localhost:4200/main.bundle.js:55401:126)
at Object.Type (http://localhost:4200/main.bundle.js:37:146)
at isValidType (http://localhost:4200/main.bundle.js:24279:157)
at http://localhost:4200/main.bundle.js:23808:25
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:4200/main.bundle.js:23806:44)
at http://localhost:4200/main.bundle.js:23819:50
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:4200/main.bundle.js:23806:44)
at http://localhost:4200/main.bundle.js:23819:50(anonymous function) @ main.bundle.js:55401(anonymous function) @ index.js:1isValidType @ metadata_resolver.js:685(anonymous function) @ metadata_resolver.js:214CompileMetadataResolver.getNgModuleMetadata @ metadata_resolver.js:212(anonymous function) @
....
....
....
metadata_resolver.js:212(anonymous function) @ metadata_resolver.js:225CompileMetadataResolver.getNgModuleMetadata @ metadata_resolver.js:212(anonymous function) @ metadata_resolver.js:225CompileMetadataResolver.getNgModuleMetadata @ metadata_resolver.js:212(anonymous function) @ metadata_resolver.js:225
After ten days of debugging the only thing that resolved this for me was switching to an inline template from templateUrl. This issue does not occur with systemjs, only with webpack build. In a systemjs build I had the following:
- Index.ejs has selector from the root app component which is bootstrapped by NgModule.
- The root app component's template has two entries, first the router outlet and then the chrome components selector. - The chrome component has two routerLink entries for additional component views.
First I checked for unused methods in my solution per the initial comments from @bvaughn above, and this had no effect. I also checked each component for circular references. I then started yanking away functionality until I was down to just trying to bootstrap the main app component, and still had the error. I started a fresh app directory on my host and redeployed in case a cached value was the issue. All throughout webpack was building successfully, but Chrome spewed call stack errors while Firefox said I had 'too much recursion.' Lines in the error messages referencing webpack's outputted file were generally around creating either the module or app view prototype.
Since this occurs only with webpack I logged a new issue there linking back to this thread #3334.
npm install angular2-template-loader --save-dev
add angular2-template-loader to webpack config ts loaders.
The loader depend on node type(require), if not config @type/node , follow:
npm install @type/node --save-dev
edit tsconfig.json, add follow config:
"types": ["node"]
We had this issue and was able to resolve it by taking out the "selector: 'app'. We're using the angluar-cli and webpack. Weird.
