Nativescript-angular: ReferenceError: Can't find variable: setTimeout [@angular 7.2.x ]

Created on 23 Jan 2019  路  9Comments  路  Source: NativeScript/nativescript-angular

While using nativescript-angular@next and @[email protected] this app is failing with

JavaScript error:
file:///app/tns_modules/@angular/core/bundles/core.umd.js:1668:19: JS ERROR ReferenceError: Can't find variable: setTimeout
*** JavaScript call stack:
(
)
*** Terminating app due to uncaught exception 'NativeScript encountered a fatal error: ReferenceError: Can't find variable: setTimeout
at
1   @file:///app/tns_modules/@angular/core/bundles/core.umd.js:1668:19
2   @file:///app/tns_modules/@angular/core/bundles/core.umd.js:8:75
3   anonymous@file:///app/tns_modules/@angular/core/bundles/core.umd.js:11:2
4   evaluate@[native code]
5   moduleEvaluation@[native code]
6   promiseReactionJob@[native code]
7   require@[native code]
8   anonymous@file:///app/main.js:4:21
9   evaluate@[native code]
10  moduleEvaluation@[native code]
11  promiseReactionJob@[native code]
', reason: '(null)'

The code involved in this error is located in @angular/core/bundles/core.umd.js:1668:19

var defaultScheduler = (typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame || // browser only
    setTimeout // everything else
).bind(_global);

Reproducible with this app

NOTE: the error is not happening when building with AOT!

Most helpful comment

I had this too, but it was caused by me moving/ordering the imports in my main.ts, so that @angular/core was imported before nativescript-angular/platform.

All 9 comments

I've add the same issue and I think that changing the compilerOptions > module config to commonjs at the tsconfig.tns.json file will fix the issue.

I had this too, but it was caused by me moving/ordering the imports in my main.ts, so that @angular/core was imported before nativescript-angular/platform.

Thx !!!
It's a bad day for auto-formatting code ;-)

I had this too, but it was caused by me moving/ordering the imports in my main.ts, so that @angular/core was imported before nativescript-angular/platform.

As @m-abs the issue is related to the case where the @angular.core is imported before nativescript-angular/platform. Simply changing the order of those imports is resolving the case. Closing the issue as this is, in fact, the way we are generating projects (with the proper import order) and a mandatory requirement for AOT builds.

Hi ,

I am still getting this issue even though I do not see @angular/core inserted in my main.tns.ts file

Versions:

    "nativescript-angular": "~7.2.0",
    "nativescript-theme-core": "~1.0.4",
    "reflect-metadata": "~0.1.12",
    "tns-core-modules": "~5.2.0",
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",

main.ts file

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

main.tns.ts file

// this import should be first in order to load some required settings (like globals and reflect-metadata)
import { platformNativeScriptDynamic } from 'nativescript-angular/platform';

Error

***** Fatal JavaScript exception - application has been terminated. *****
2019-02-26 18:14:50.079 nsplaydev[2296:935692] Native stack trace:
1   0x101339194 NativeScript::reportFatalErrorBeforeShutdown(JSC::ExecState*, JSC::Exception*, bool, bool)
2   0x10136f720 -[TNSRuntime executeModule:referredBy:]
3   0x102213b78 -[UIApplication(PlayLiveSync) swizzled_executeModule:]
4   0x100c7b4f4
5   0x1c7162bb4 <redacted>
2019-02-26 18:14:50.081 nsplaydev[2296:935692] JavaScript stack trace:
2019-02-26 18:14:50.081 nsplaydev[2296:935692] 1   @file:///app/tns_modules/@angular/core/bundles/core.umd.js:2029:19
2   @file:///app/tns_modules/@angular/core/bundles/core.umd.js:8:75
3   anonymous@file:///app/tns_modules/@angular/core/bundles/core.umd.js:11:2
4   evaluate@[native code]
5   moduleEvaluation@[native code]
6   promiseReactionJob@[native code]
7   require@[native code]
8   @angular/core@file:///app/bundle.js:1852:25
9   __webpack_require__@file:///app/bundle.js:76:34
10  ./main.ts@file:///app/bundle.js:1805:90
11  __webpack_require__@file:///app/bundle.js:76:34
12  checkDeferredModules@file:///app/bundle.js:45:42
13  @file:///app/bundle.js:134:38
14  anonymous@file:///app/bundle.js:135:12
15  evaluate@[native code]
16  moduleEvaluation@[native code]
17  promiseReactionJob@[native code]
18  require@[native code]
19  anonymous@file:///app/starter.js:2:8
20  evaluate@[native code]
21  moduleEvaluation@[native code]
22  promiseReactionJob@[native code]
2019-02-26 18:14:50.081 nsplaydev[2296:935692] JavaScript error:
**file:///app/tns_modules/@angular/core/bundles/core.umd.js:2029:19: JS ERROR ReferenceError: Can't find variable: setTimeout**
2019-02-26 18:14:50.083 nsplaydev[2296:935692] PlayLiveSync: Uncaught Exception
2019-02-26 18:14:50.083 nsplaydev[2296:935692] *** JavaScript call stack:
(

)
2019-02-26 18:14:50.083 nsplaydev[2296:935692] *** Terminating app due to uncaught exception **'NativeScript encountered a fatal error: ReferenceError: Can't find variable: setTimeout**
 at
1   @file:///app/tns_modules/@angular/core/bundles/core.umd.js:2029:19
2   @file:///app/tns_modules/@angular/core/bundles/core.umd.js:8:75
3   anonymous@file:///app/tns_modules/@angular/core/bundles/core.umd.js:11:2
4   evaluate@[native code]
5   moduleEvaluation@[native code]
6   promiseReactionJob@[native code]
7   require@[native code]
8   @angular/core@file:///app/bundle.js:1852:25
9   __webpack_require__@file:///app/bundle.js:76:34
10  ./main.ts@file:///app/bundle.js:1805:90
11  __webpack_require__@file:///app/bundle.js:76:34
12  checkDeferredModules@file:///app/bundle.js:45:42
13  @file:///app/bundle.js:134:38
14  anonymous@file:///app/bundle.js:135:12
15  evaluate@[native code]
16  moduleEvaluation@[native code]
17  promiseReactionJob@[native code]
18  require@[native code]
19  anonymous@file:///app/starter.js:2:8
20  evaluate@[native code]
21  moduleEvaluation@[native code]
22  promiseReactionJob@[native code]
', reason: '(null)'
*** First throw call stack:
(0x1c7718ec4 0x1c68e9a40 0x101339544 0x10136f720 0x102213b78 0x100c7b4f4 0x1c7162bb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
2019-02-26 18:14:50.084 nsplaydev[2296:935692] PlayLiveSync: Uncaught Exception

@Rugved try to update your dependencies - I am using the latest one in a code shared project and the issue is not present. Here is my package.json

  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/http": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "core-js": "^2.5.4",
    "nativescript-angular": "~7.2.0",
    "nativescript-theme-core": "~1.0.4",
    "reflect-metadata": "~0.1.12",
    "rxjs": "~6.3.3",
    "tns-core-modules": "~5.2.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/cli": "^7.2.0",
    "@angular/compiler-cli": "~7.2.0",
    "@angular-devkit/build-angular": "~0.11.4",
    "@nativescript/schematics": "~0.4.0",
    "@types/jasmine": "2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "nativescript-dev-typescript": "~0.8.0",
    "nativescript-dev-webpack": "^0.20.0",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~3.1.1"
  }

Plus the latest version of NativeScript

Yup that worked, thanks.

This just affected us on @angular 8.2.1 fyi, and the dependency order fix solved it. Pasting relevant deps below for ref.

"tns-ios": {
      "version": "6.1.1"
    },
    "tns-android": {
      "version": "6.1.2"
    },
   "@angular/animations": "8.2.1",
    "@angular/common": "8.2.1",
    "@angular/compiler": "8.2.1",
    "@angular/core": "8.2.1",
    "@angular/forms": "8.2.1",
    "@angular/platform-browser": "8.2.1",
    "@angular/platform-browser-dynamic": "8.2.1",
    "@angular/router": "8.2.1",
    "nativescript-angular": "8.2.1",

I had this too, but it was caused by me moving/ordering the imports in my main.ts, so that @angular/core was imported before nativescript-angular/platform.

thanks a lot man, this problem was driving me crazy.

Was this page helpful?
0 / 5 - 0 ratings