Ionic - Uncaught SyntaxError: Unexpected token On APK

Viewed 2952

I have an error since yesterday I try to fix. I successfully built an android apk using:

ionic cordova build android --prod --release

But the app stuck on white screen on device and emulator (GenyMotion). I inspected the logcat of GenyMotion. I figured out these:

>  I/chromium( 5497): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token )", source: http://localhost/runtime.fcad956485f9614ff8b1.js (1)
>  D/SystemWebChromeClient( 5497): http://localhost/polyfills.49d987a1d15d60566c3f.js: Line 1 : Uncaught  SyntaxError: Unexpected token =>
> I/chromium( 5497): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token =>", source: http://localhost/polyfills.49d987a1d15d60566c3f.js (1)
> D/SystemWebChromeClient( 5497): http://localhost/main.2be168d95557cbdc0a3f.js: Line 1 : Uncaught SyntaxError: Unexpected token =>

> I/chromium( 5497): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token =>", source: http://localhost/main.2be168d95557cbdc0a3f.js (1)

I don't know why. They are no errors when building.

My package.json

... 
"@angular/common": "~12.0.1",
"@angular/core": "~12.0.1",
"@angular/forms": "~12.0.1",
"@angular/platform-browser": "~12.0.1",
"@angular/platform-browser-dynamic": "~12.0.1",
"@ionic/angular": "^5.5.2",
"ionic": "^5.4.16",
"cordova-android": "^9.0.0",
"cordova-browser": "^6.0.0",
"cordova-ios": "^5.1.1",
"cordova-plugin-ionic-webview": "^5.0.0",
... 
"@typescript-eslint/eslint-plugin": "4.16.1",
"@typescript-eslint/parser": "4.16.1",
"typescript": "~4.2.4"

My tsconfig.json.

...  
"target": "es2015", 
"module": "es2020",
"lib": ["es2018", "dom"] 
...  
3 Answers

After a real battle in order to fix the issue since 3 days, i solved the problem. Ionic 5 angular 12 use es2015 as a target when building. I set "target" to es5 in tsconfig.json and errors in runtime.**.js, polyfills.**.json disappeared

es5 transform ES6 function to standard functions.

More info at https://www.typescriptlang.org/tsconfig

Here is pretty clear. You have a syntax error. I suppose you can check logs for ionic cordova build android --prod. Probably It will show which files and lines generate these problems.

If you use linter you can try to run npm run lint to check any problems with your code.

ionic + angular has update the type script compiler in the recent update as Root folder > tcconfig.json > compilerOptions > target > es2020 - This is causing the issue in android version 29

Chang the target as es2015 to resolve the issue

Related