Nativescript: Still getting __metadata is not defined using --env.roduction on Android

Created on 2 Oct 2020  Â·  10Comments  Â·  Source: NativeScript/NativeScript

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

✔ Getting NativeScript components versions information...
✔ Component nativescript has 7.0.8 version and is up to date.
✔ Component @nativescript/core has 7.0.9 version and is up to date.
✖ Component @nativescript/ios is not installed.
✔ Component @nativescript/android has 7.0.0 version and is up to date.

"dependencies": {
"@nativescript-community/ui-pager": "^13.0.0-alpha.10",
"@nativescript/core": "~7.0.9",
"@nativescript/firebase": "^11.0.0",
"@nativescript/iqkeyboardmanager": "^2.0.0",
"@types/node": "^12.12.7",
"moment": "^2.27.0",
"moment-timezone": "^0.5.28",
"@nativescript/appversion": "^2.0.0",
"nativescript-audio-player": "^1.0.3",
"@nativescript/geolocation": "^7.0.0",
"nativescript-insomnia": "^2.0.0",
"nativescript-mqtt": "^1.2.0",
"@nativescript/secure-storage": "^3.0.0",
"@nativescript/theme": "^3.0.0",
"nativescript-ui-listview": "^9.0.2",
"nativescript-ui-sidedrawer": "^9.0.3"
},
"devDependencies": {
"@nativescript/android": "7.0.0",
"@nativescript/types": "7.0.0",
"@nativescript/webpack": "~3.0.0",
"node-sass": "^4.14.1",
"tslint": "^6.1.1",
"typescript": "3.9.7"
},

Describe the bug
The app runs fine until I add --env.production. I then get:
System.err: java.lang.RuntimeException: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function
System.err: ReferenceError: __metadata is not defined

May be related to the bugs referenced in #8903 but that was apparently fixed.

To Reproduce
ns run android --emulator --env.production

Expected behavior
app runs

I did also make sure to run ns clean to make sure my build was fresh.

Most helpful comment

@kryptus36 managed to get your project to work. It was a tree shaking issue. PR is already here https://github.com/NativeScript/NativeScript/pull/8931

All 10 comments

@kryptus36 by any chance can you share your project ? If not maybe your app/main.ts ? It would help in figuring out why it still crashes

@farfromrefug I could provide access privately to a few people.

It is happening in an odd spot in the code. So you may need to see it. It's in here, line 5 when @ObservableProperty is called:

`import { Observable } from '@nativescript/core';
import { ObservableProperty } from '../../../shared/observable-property-decorator';

export class BPCardSpace extends Observable {
@ObservableProperty() number: number;
@ObservableProperty() daubed: boolean;

constructor(obj: any) {
super();

this.number = obj.number;
this.daubed = obj.daubed;

}
}
`

Here's my app.ts:

`import { getErrorService } from './globals/dependencies/locator';
import { setApplicationEvents } from './globals/app-events/app-events';
import { Trace, Application } from '@nativescript/core';

/*
Firebase init
What if iOS doesn't show/receive notifications in the background?
Make sure you require the plugin in app.ts / main.ts / main.aot.ts before application.start(),
and do init() after the app has started (not in app.ts - not even in a timeout; move it out of app.ts entirely!).

Make sure to use require, not import in app.ts because TSC will remove the import in case you're not using it.
*/
const firebase = require('@nativescript/firebase');

const errorHandler: Trace.DefaultErrorHandler = {
handlerError(err: Error) {
if (global.TNS_ENV === 'dev') {
throw err;
} else if (global.TNS_ENV === 'prod') {
const errorService = getErrorService();
errorService.reportError(err);
}
},
};
Trace.setErrorHandler(errorHandler);

setApplicationEvents();

Application.run({ moduleName: 'app-root' });

/*
Do not place any code after the application has been started as it will not
be executed on iOS.
*/
`

Ok I think it happens because of.the first 2 lines of your app.ts.
I could fix it if you give me access to your repo. If you are ok with it

I'll send an invite to your email.

@farfromrefug it's set up and sent to you. Hopefully you got it.

@kryptus36 managed to get your project to work. It was a tree shaking issue. PR is already here https://github.com/NativeScript/NativeScript/pull/8931

Thanks to @farfromrefug this is now released in 7.0.10:
https://github.com/NativeScript/NativeScript/releases/tag/7.0.10-core

@kryptus36 just please confirm it works for you. @NathanWalker i think we can now remove the imports of globals all around the core? (Except maybe android activity, I am not sure it is not still necessary there )

@farfromrefug Thanks very much, this is now working for me using "@nativescript/core": "~7.0.10"

@farfromrefug most likely we can as sideEffects should handle things but let’s keep things way they are for a bit and can confirm privately to ensure any further change is all set.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nirsalon picture nirsalon  Â·  3Comments

tsonevn picture tsonevn  Â·  3Comments

fmmsilva picture fmmsilva  Â·  3Comments

rLoka picture rLoka  Â·  3Comments

OscarLopezArnaiz picture OscarLopezArnaiz  Â·  3Comments